Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i am new to WP plugins, and i have made one that auto creates posts and catagories.

Anyhow, id like to know if i can set the meta values when creating the post such as:

        $my_post_data = array(
            'post_title' => $collection,
            'post_type' => 'post',
            'post_content' => '<p style="margin-bottom: 10px;">' . $content . '</p>',
            'post_category' => array(
                $term->term_id
            ),
            'post_author' => 1,
            'post_status' => 'publish',
            'post_date' => date('Y-m-d H:i:s')
        );

Is there any way i can set the title and description for this page?

share|improve this question

1 Answer 1

When you use wp_insert_post to create the post, it will return the post id of the post you just created. The following code demonstrate how to add meta values:

$my_post_data = array(
            'post_title' => $collection,
            'post_type' => 'post',
            'post_content' => '<p style="margin-bottom: 10px;">' . $content . '</p>',
            'post_category' => array(
                $term->term_id
            ),
            'post_author' => 1,
            'post_status' => 'publish',
            'post_date' => date('Y-m-d H:i:s')
        );
$post_id = wp_insert_post( $my_post_data );

if ( ! $post_id && ! is_wp_error( $post_id ) ) {
    add_post_meta( $post_id, 'title', 'This is a title' );
    add_post_meta( $post_id, 'description', 'This is a description' );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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