我有一个联盟产品饲料,其中包含许多重复的产品。SKU是不同的,但它是相同的形象,标题和描述。我正在使用WPallimport。
有没有人有检查数据库中重复标题的代码?
发布于 2020-06-15 11:39:44
文档中也有类似的代码片段(见下文)。您需要修改它以检查标题,而不是自定义字段。
还要注意对导入ID的额外检查-您还需要更新它(或删除它)。
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);https://stackoverflow.com/questions/62375789
复制相似问题