Home > CMS Tutorial > WordPress > body text

Detailed explanation of how to add articles in batches through WordPress built-in functions

藏色散人
Release: 2021-02-09 17:19:56
forward
3748 people have browsed it

The following WordPress tutorial column will introduce to you how to add articles in batches through WordPress’ built-in functions. I hope it will be helpful to friends in need!

Detailed explanation of how to add articles in batches through WordPress built-in functions

Recently, my business needs to add a large number of articles in batches to the website. Manually adding articles one by one would definitely kill me, so I started looking for a way to add them in batches. In fact, the relevant content of the article is already in the local database. The first method I thought of was to directly import data into the online library through SQL statements.

So I inserted the data into the online table through

INSERT INTO target_table (key1, key2...) SELECT key1', key2' ... FROM source_table;
Copy after login

. When I opened the page, I saw it was all gibberish. So, I set the encoding again before inserting, but there was still a problem.

Since I am not good at SQL, I adjusted my strategy. By chance, I discovered a WordPress built-in function ‘wp_insert_post’. Well, that's him.

So, I exported the target data into php_array, then introduced it into my script, and added it to the database through the wp_insert_post function.

foreach( $php_array as $item ){
    $arg = array(
        'post_title' => $item['title'],
        'post_content' => $item['content'],
        'post_excerpt' => $item['excerpt'],
        'post_type' => 'post',
        'post_status' => 'public',
        'meta_input' => array(
            'meta_key' => 'meta_value'
        )
    );
    wp_insert_post( $arg );
}
Copy after login

In this way, articles are added in batches through WordPress in a "legal" way, and custom columns can be added to meta_input, which can be said to be great.

For specific usage methods of wp_insert_post, please refer to: Official Documentation

https://developer.wordpress.org/reference/functions/wp_insert_post/

The above is the detailed content of Detailed explanation of how to add articles in batches through WordPress built-in functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template