Hi,
I have this csv-file which I want to filter during the import process in WPAI.
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
During the import process in wpai you set the unique identifier. Us this field in the import as your identifier.
Hi John,
yes that would do the trick, if I only use that field as my unique identifier.
But since I’ll have more than just that field in my unique identifier, that won’t work (have to use more than one field in my unique identifier, since I am trying to combine different csv-files into one big shop).
Is there no way, without using the unique identifier? Cant the code above be used, to filter it out during import?
Thanks for your time!
The topic ‘Only create a new post if an existing custom field value doesn't exist’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.