WordPress: wp_update_post doesn’t update the post with new data from a CSV

I’m writing some code that will read a csv with information about real estate listings, and either create a post for that listing or update the current post. The title of each listing is the address, so if it detects a post with that address already made, I want it to update the current post. The posts are of a custom type “listing” with custom fields such as “_listing_address” (also the post_title) and “_listing_price”, etc. Here is the part of the code that initializes the array for a new post or with data to update the new post:

$new_post = array(
        'post_title'   => convert_chars($data['csv_post_title']),
        'post_content' => wpautop(convert_chars($data['csv_post_post'])),
        'post_status'  => $opt_draft,
        'post_type'    => $type,
        'post_date'    => $this->parse_date($data['csv_post_date']),
        'post_excerpt' => convert_chars($data['csv_post_excerpt']),
        'post_name'    => $data['csv_post_slug'],
        'post_author'  => $this->get_auth_id($data['csv_post_author']),
        'tax_input'    => $this->get_taxonomies($data),
        'post_parent'  => $data['csv_post_parent'],
);

And here is the code that either creates a new post or (should) update an existing post:

Read More
if (!get_page_by_title( $new_post['post_title'], 'OBJECT', 'listing')) {
    $id = wp_insert_post($new_post);
} else {
    $old_post = get_page_by_title( $new_post['post_title'], 'OBJECT', 'listing' );
    $new_post['ID'] = $old_post->ID;
    $id = wp_update_post($new_post);
}

I can successfully create new posts with the code that I have, and it also successfully checks if another post has the same title, since it doesn’t create duplicate posts. However, it doesn’t actually update the posts. So if on my csv file I change the price of one of the listings from $29,900 to $30,000, when I re-upload the csv, it won’t create a new post since the address didn’t change, but it won’t change price of the current post. I’m guessing it’s a problem with the post ID’s, but anything I’ve tried hasn’t worked. Any help would be greatly appreciated.

Other relevant information:
I’m using the CSV Importer plugin for WordPress, which I modified with the above code to check for duplicate posts and update them. I’m also using the Genesis framework and the Agentpress theme, which creates the listing custom type.

Related posts

1 comment

  1. I figured it out. The problem was that the code I had above creating an array didn’t deal with any custom fields – those were handled by another function called later on, which used the create_post_meta() function for WordPress to create the custom fields. Obviously this was a problem for me since I was trying to update post meta fields that had already been created. All I had to do was switch this to update_post_meta(), which both updates already created post meta or creates a new one if it hasn’t. Hopefully this helps anyone who is using CSV Importer or has made their own CSV upload plugin and is struggling to both create and update posts from a CSV file.

Comments are closed.