【问题标题】:Wpallimport check duplicate by titleWpallimport 按标题检查重复
【发布时间】:2020-06-14 17:11:56
【问题描述】:

我有一个包含大量重复产品的附属产品供稿。 SKU 不同,但图像、标题和描述相同。我正在使用 WPallimport。

有没有人有检查数据库是否有重复标题的代码?

【问题讨论】:

    标签: xml wordpress woocommerce feed wpallimport


    【解决方案1】:

    文档中有类似的代码 sn-p(见下文)。您需要对其进行修改以检查标题而不是自定义字段。

    还要注意对导入 ID 的额外检查 - 您还需要更新它(或删除它)。

    原始sn-p:https://www.wpallimport.com/documentation/developers/code-snippets/#only-create-a-post-if-an-existing-post-doesnt-have-the-same-custom-field-value

    function create_only_if_unique_custom_field( $continue_import, $data, $import_id )
    {
        // Only run for import ID 1.
        if ($import_id == 1) {
    
            // The custom field to check.
            $key_to_look_up = "my_custom_field";
    
            // The value to check where 'num' is the element name.
            $value_to_look_up = $data['num'];
    
            // Prepare the WP_Query arguments
            $args = array (
    
                // Set the post type being imported.
                'post_type'  => array( 'post' ),
    
                // Check our custom field for our value.
                'meta_query' => array(array(
                'key'        => $key_to_look_up,
                'value'      => $value_to_look_up,
                )),
            );
    
            // Run the query and do not create post if custom
            // field value is duplicated.
        $query = new WP_Query( $args );
        return !($query->have_posts());
    
        } else {
    
            // Take no action if a different import ID is running.
           return $continue_import;
    
        }
    }
    
    add_filter('wp_all_import_is_post_to_create', 'create_only_if_unique_custom_field', 10, 3);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 2020-11-09
      相关资源
      最近更新 更多