我有一个联盟产品Feed,其中包含很多重复的产品。 SKU不同,但图像,标题和说明相同。我正在使用WPallimport。
有人有没有代码检查数据库的重复标题?
我有一个联盟产品Feed,其中包含很多重复的产品。 SKU不同,但图像,标题和说明相同。我正在使用WPallimport。
有人有没有代码检查数据库的重复标题?
文档中有一个类似的代码片段(请参见下文)。您需要对其进行修改以检查标题而不是自定义字段。
还请注意导入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);