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.

2 Answers 2


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.

  • Ohh, the wp im testing on is 4.3, thanks a lot.
    – streel
    Jan 30, 2016 at 14:15

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) { 
    //'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);   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.