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 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答