dongtanlin0765 2015-07-22 19:32
浏览 56
已采纳

Wordpress:wp_update_post不会使用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:

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.

  • 写回答

1条回答 默认 最新

  • dongmo2324 2015-07-23 15:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题