Home >CMS Tutorial >WordPress >Detailed explanation of how to add articles in batches through WordPress built-in functions

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

藏色散人
藏色散人forward
2021-02-09 17:19:563786browse

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;

. 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 );
}

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete