首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wpallimport按标题检查重复

Wpallimport按标题检查重复
EN

Stack Overflow用户
提问于 2020-06-15 01:11:56
回答 1查看 460关注 0票数 1

我有一个联盟产品饲料,其中包含许多重复的产品。SKU是不同的,但它是相同的形象,标题和描述。我正在使用WPallimport。

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

EN

回答 1

Stack Overflow用户

发布于 2020-06-15 11:39:44

文档中也有类似的代码片段(见下文)。您需要修改它以检查标题,而不是自定义字段。

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

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

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62375789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档