How to insert post meta values in wordpress

How to insert post meta values while adding a post? I am using wp_insert_post function to insert the post. For using the "add_post_meta" function, we need to specify the post ID. How can I add the values at the time of inserting post? Is there any way to do this!?

regards - dj


i suppose you have created an array to insert post

$new_post = array(
        'post_title'   => $title,
        'post_content' => $content,
        'post_type'    => $type,
    'post_status'  => $status           

get the new inserted post_id with

$id = wp_insert_post($new_post);

then insert values in wp_postmeta table by passing parameters like post_id, meta_key, meta_value


try it.

Sorry for the confusion.I found the solution myself. We need to use the Post ID that returns by the function wp_insert_post. It returns the post ID value for the successful posting.

Need Your Help

Why doesn't Python require exactly four spaces per indentation level?

python indentation

Whitespace is signification in Python in that code blocks are defined by their indentation.