Hi,
I have this csv-file which I want to filter during the import process in WPAI.
https://imgur.com/V73hnZL
For example: In this csv-file (see image) are two products, hat and pants, products with the same product name (csv-column = product_name1), but in different colors.
My goal is that the first row of a product gets imported and the other rows with the same product name get skipped.
I’m using the WPAI example code (create_only_if_unique_custom_field ) with my ACF-Custom Field with holds the “product_name1” value, like hat or pants to filter it out.
But instead of only tow rows getting imported as posts I end up with 8 posts: 4x Posts with the WP-title “hat”, “hat-2”, “hat-3”, “hat-4”
4x Posts with the WP-title “pants”, “pants-2”, “pants-3”, “pants-4”
What do I need to change in my code:
————————————-
/**
* Only allow creation of a new post if an existing custom field value
* doesn’t already exist.
*/
function create_only_if_unique_custom_field( $continue_import, $data, $import_id )
{
// Check for your import
if ($import_id == 1) {
$key_to_look_up = the_field(‘namecheck’); // My ACF-Custom Field
$value_to_look_up = $data[‘product_name1’]; // Column from the CSV-File
$args = array (
‘post_type’ => array( ‘post’ ),
‘meta_query’ => array(array(
‘key’ => $key_to_look_up,
‘value’ => $value_to_look_up,
)),
);
$query = new WP_Query( $args );
return !($query->have_posts());
}
else {
return true;
}
}
add_filter(‘wp_all_import_is_post_to_create’, ‘create_only_if_unique_custom_field’, 10, 3);
Thank you,
Best Regards, Tobias