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 设计一个温度闭环控制系统
  • ¥100 关于加载卡的问题有能知道这个要怎么处理吗?
  • ¥100 rtmpose姿态评估
  • ¥15 java 通过反射找路径下的类,打包后就找不到
  • ¥15 通联支付网上收银统一下单接口
  • ¥15 angular有偿编写,
  • ¥15 centos7系统下abinit安装时make出错
  • ¥15 hbuildex运行微信小程序报错
  • ¥15 关于#python#的问题:我知道这个问题对你们来说肯定so easy
  • ¥15 wpf datagrid如何实现多层表头