WordPress.org

Support

Support » Plugins and Hacks » wp_insert_post ADD post_meta data

wp_insert_post ADD post_meta data

  • supahoopsa
    Member

    @supahoopsa

    This is a cutdown version of the code I am using to pull data from a database and insert into WP as a post

    <?php
    
    require('wp-blog-header.php');
    
    $post_title = $title;
    $post_status = 'publish';
    $post_date= current_time('mysql');
    $post_date_gmt= current_time('mysql', 1);
    $post_author=1;
    
    $post_content="Blahblahblah";
    
    $category_slug = $country; 
    
    $post_category=array($category_ID);
    
    $tags_input= array($mytags);
    
    $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status', 'tags_input');
    $post_data = add_magic_quotes($post_data);
    $post_ID = wp_insert_post($post_data);
    
    if ( is_wp_error( $post_ID ) )
    echo "\n" . $post_ID->get_error_message();
    }
    ?>

    My question is…

    I now need to populate a custom data field in wp_postmeta

    Custom field will be called “photourl” and data held in script as $picurl

    Is there an easy way of doing this ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • supahoopsa
    Member

    @supahoopsa

    No replies 🙁

    Is his harder than I’d imagined

    I was looking around for modifying the date properly on inserting posts and found this old post. I hope you figured it out! But if not, it is very simple and all you need is the post id and you get it wit the above code.

    $post_ID will be the post id from the code above and to update post meta just do this

    update_post_meta($post_ID, 'custom_field_name', $custom_field_item);

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_insert_post ADD post_meta data’ is closed to new replies.