duandu1915 2013-07-19 13:53 采纳率: 100%
浏览 37
已采纳

Wordpress csv导入重复

I've used an adapted version of a csv import plugin for wordpress. It imports the csv to create and update posts. To update existing posts I've added an edit that finds the post id by title, then if it exists it overwrites, otherwise it creates a new post.

Problem is that it seems to not be very reliable! If I import the same csv several times it over-writes most of them but I get a few duplicates. Is there any way I can make this more reliable, or is there another way I can deal with duplicate posts?

main function for creating posts here

 function create_post($data, $options) {
    extract($options);
//edit 1 added here
   global $wpdb;  
//end

    $data = array_merge($this->defaults, $data);
    $type = $data['csv_post_type'] ? $data['csv_post_type'] : 'post';
    $valid_type = (function_exists('post_type_exists') &&
        post_type_exists($type)) || in_array($type, array('post', 'page'));

    if (!$valid_type) {
        $this->log['error']["type-{$type}"] = sprintf(
            'Unknown post type "%s".', $type);
    }

    $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'],
    );

// edit 2 here
    $new_post['ID'] = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" .   $data['csv_post_title'] . "'" );
// ends

    // pages don't have tags or categories
    if ('page' !== $type) {
        $new_post['tags_input'] = $data['csv_post_tags'];

        // Setup categories before inserting 
        $cats = $this->create_or_get_categories($data, $opt_cat);
        $new_post['post_category'] = $cats['post'];
    }

// edit 3
    if(!empty($new_post['ID'])) {
    $id = wp_update_post($new_post);
} else {
    $id = wp_insert_post($new_post);
}
// ends

    if ('page' !== $type && !$id) {
        // cleanup new categories on failure
        foreach ($cats['cleanup'] as $c) {
            wp_delete_term($c, 'category');
        }
    }
    return $id;
}

thanks

  • 写回答

1条回答 默认 最新

  • dsaj20411 2013-07-22 15:57
    关注

    Fixed! I used the built in wordpress function, get_page_by_title . Here is the code I used to target the correct post.

    if (!get_page_by_title( $bpp_title, 'OBJECT', 'publications')) {
    
    
        $id = wp_insert_post($new_post);
    
        } else {
    
            $bpp_page = get_page_by_title( $bpp_title, 'OBJECT', 'publications');
            $new_post['ID'] = $bpp_page->ID;
    
            $id = wp_update_post($new_post);
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大