$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom field - What is "meta_input" parameter in wp_insert_post() used for?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom field - What is "meta_input" parameter in wp_insert_post() used for?

matteradmin10PV0评论

I am inserting some post into wordpress using the function wp_insert_post().

I want to insert some custom fields on each post and reading the documentation I though the meta_info parameter was used for that, I tried something like this:

$data = array(
        'post_author' => 1,
        'post_status' => 'publish',
        'post_title' => $post->getTitle(),
        'post_content' => $post->getContent(),
        'post_category' => $post->getCategory(),
        'tags_input' => $post->getTags(),
        'meta_input' => array( "_test" => "testx1" )
);

$postID = wp_insert_post( $data );

The post gets inserted correctly and tags too. But there are no custom fields added. I know I could use add_post_meta() to add them but I still would like to know what the meta_input parameter is used for, because I did a search on the database for "testx1" after inserting the post and couldn't find any result.

I am inserting some post into wordpress using the function wp_insert_post().

I want to insert some custom fields on each post and reading the documentation I though the meta_info parameter was used for that, I tried something like this:

$data = array(
        'post_author' => 1,
        'post_status' => 'publish',
        'post_title' => $post->getTitle(),
        'post_content' => $post->getContent(),
        'post_category' => $post->getCategory(),
        'tags_input' => $post->getTags(),
        'meta_input' => array( "_test" => "testx1" )
);

$postID = wp_insert_post( $data );

The post gets inserted correctly and tags too. But there are no custom fields added. I know I could use add_post_meta() to add them but I still would like to know what the meta_input parameter is used for, because I did a search on the database for "testx1" after inserting the post and couldn't find any result.

Share Improve this question edited Jan 30, 2016 at 13:56 streel asked Jan 30, 2016 at 13:49 streelstreel 1051 gold badge1 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 7

This part of wp_insert_posts() gives it away:

  if ( ! empty( $postarr['meta_input'] ) ) {
        foreach ( $postarr['meta_input'] as $field => $value ) {
            update_post_meta( $post_ID, $field, $value );
        }
  } 

where we see how the post meta fields are updated/added with update_post_meta().

Here's the inline description for meta_input:

Array of post meta values keyed by their post meta key. Default empty.

This was added in WordPress 4.4 and here's relevant ticket #20451 for more information.

Note that using the underscore in front of the the meta key _test will hide it from the custom fields metabox in the post edit screen.

The way I do it is via term_id not slug and it works:

//insert Art items into database
$arr = array('item 1', 'item 2');
// $arr = array('art item 1', 'art item 2');

foreach ($arr as $a) { 
    wp_insert_post(array(
    //essentials
    //'ID'      => 1131,
    'post_author'       => 1,
    'post_title'        => $a,
    'post_type'         => 'post',
    'post_content'      => 'Something...',
    'post_status'       => 'publish',
    'post_name'         => 'post name',
    'meta_input'        => array( //(array) Array of post meta values keyed by their post meta key. Default empty.
        'city'     => '',// 'name' => $post['name']
        'country'  => ''// 'city' => $post['city']
    ),
    'tax_input'    => array(
        'category' => array(33,32), //id numbers work, slugs tend to be ignored !!!
        'post_tag' => array('one', 'two') //for tags slugs seem to work
    ),//(array) Array of taxonomy terms keyed by their taxonomy name. Default empty. Equivalent to calling wp_set_post_terms() / wp_set_object_terms()
    //'tags_input'  => array('una', 'trei'), //(array) Array of tag names, slugs, or IDs. Default empty. Equivalent to calling wp_set_post_tags().
    ), true);   
}
Post a comment

comment list (0)

  1. No comments so far