The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

[resolved] how to add post_meta data to a newly created post - by csv import? (3 posts)

  1. vincej
    Posted 6 years ago #

    Hi, using 2.92, haven't upgraded to 3.0 yet. still building out custom site..... but expect this to work with current version.

    I have created a CSV importer for my site, it reads in a text file, and is formatted basically as
    ID, post title, post content, post tags, etc.... customfield sku, customfield price

    I read in and parse the tab-delimited file and two situations occur.
    1. if the record contains an ID then the script will update the current WordPress posting with the new info. (this part works).
    2. If the record does NOT contain an ID, then the script creates a new post, and populates the wordpress fields in the dbase. -- the problem here is I cannot figure out how to populate the custom fields with this method.

    any help please?
    The line of interest is how to add
    add_post_meta($my_post['ID'] , 'sku', $sku); to the CREATE post option, as I DO NOT KNOW HOW TO GET THE POST ID I've just created to enable the add_post_meta function to work.
    Although I include
    $my_post['price'] = $price;
    $my_post['sku'] = $sku; in the args for the NEW POST... it does not seem to insert these upon post creation. NO problem at all when UPDATING a post, though.

    here is the code specifics of the function I use so far.

    function updateGS() {
    $filename = $_SERVER["DOCUMENT_ROOT"] . '/navs/WP_update_GS.txt';
    if ($fileContents = file_get_contents($filename)) {
    $contentsArray = explode("\r", $fileContents) ;
    $numlines = count($contentsArray)-1;
    while ($i <= $numlines) {
    $thisItem = '';
    $thisItem = $contentsArray[$i];
    $atribs= explode("\t", $thisItem);
    $thetags = explode("," , $atribs[4] );
    $thetagslist = $thetags[0];
    if ($thetags[1] !='') $thetagslist .= ',' . $thetags[1] ;
    if ($thetags[2] != '') $thetagslist .= ',' . $thetags[2] ;
    $price = $atribs[5] ;
    $sku= $atribs[6] ;

    $my_post = array();
    $my_post['ID'] = $atribs[0] ;
    $my_post['post_type'] = 'post';
    $my_post['post_title'] = $atribs[1] ;
    $my_post['post_content'] = $atribs[2] ;
    $my_post['post_status'] = 'publish';
    $my_post['post_author'] = '1';
    $my_post['post_category'] = array($atribs[3]);
    $my_post['tags_input'] = $thetagslist ;
    $my_post['price'] = $price;
    $my_post['sku'] = $sku;

    // Update the post into the database
    if ($my_post['ID'] >1) {
    wp_update_post( $my_post );

    if (!update_post_meta($my_post['ID'] , 'sku', $sku)) add_post_meta($my_post['ID'] , 'sku', $sku);
    if (!update_post_meta($my_post['ID'] , 'price', $price)) add_post_meta($my_post['ID'] , 'price', $price);

    } else {
    wp_insert_post( $my_post ); }
    $i++ ;
    } } }

  2. vincej
    Posted 6 years ago #

    a quick addition: a simple method of asking the question, perhaps.

    how to get wp_insert_post( $my_post ); to return my ID for the new post.
    So far as I can see, it only returns a true or false.
    what I'd need is something like
    wp_insert_post( $my_post ) => ID result?

  3. vincej
    Posted 6 years ago #

    can you say Duh?

    problem solved.
    get the id easily by
    $newpost=wp_insert_post( $my_post );

    $newpost returns ID number, as per info found on wordpress docs.

    coulda sworn I read up before.... but must have missed this simple line.
    as per the docs:
    "This function inserts posts (and pages) in the database. It sanitizes variables, does some checks, fills in missing variables like date/time, etc. It takes an object as its argument and returns the post ID of the created post (or 0 if there is an error)."

    Thought I'd make it handy for any others who got puzzled.

    well that solves it, a nice csv import with a few lines of php and we're on our way to building a custom storefront/blog website for our biz.

    pretty cool.
    thanks for listening.

Topic Closed

This topic has been closed to new replies.

About this Topic