Home › Forums › General Issues › Deleting values in one Relationship field based on Values in another
Problem: Delete wish list items on a Consumer record if those items are saved to a sales Catalog. Consumer, Catalog, and Items (Daylilies) are all CPTs. Wish List and Catalog Items are Relationship fields. I wanted to delete items from a consumer wish list if those items were saved to a catalog associated to that consumer. My code is below. I followed the code in the Bidirectional Relationships tutorial, although my process was slightly different. It took me a while to get this to work, but I was so jived when it did. The gotcha for me was not to use the filter, but to trigger off the save_post action of the new or updated catalog record.
Disclaimer: The code could probably use some tweaking. I am providing it because I couldn’t find many examples of coding against relationship fields. It’s just an example. Have fun with it.
Consumer post has wish list items stored in a Relationship field.
Catalog post references consumer and has catalog items in a Relationship field.
If Catalog item matched Wish List item for that Consumer, delete the Wish List Item.
add_action(‘save_post’, ‘daylilies_save_catalog_check_wish_list’,10,3);
function daylilies_save_catalog_check_wish_list($post_id){
// Don’t run this code if this post is not a catalog post type
if(! $post_id){
return;
}
// If there is a post IdD Determine the Post Type
if($post_id){
$post_post_type = get_post_type($post_id);
}
// Return if anything but a catalog post type
if(“catalog” != $post_post_type){
return;
}
// Call the function that will remove wish list items if they match items in this catalog
daylilies_remove_items_from_wish_list($post_id);
return;
}
So, if post being saved is a catalog post, let’s do some work:
function daylilies_remove_items_from_wish_list($catalogID){
// Run this function after saving a catalog
// Function will remove catalog items from a consumer wish list
// if that consumer is associated with the catalog being saved
// Look to see if this catalog has an associated consumer
$this_catalog_consumer = get_field(‘dl_catalog_consumer’, $catalogID);
// If there is a consumer, grab the array of catalog items
// Populate another array with the wish list items from this consumer
if($this_catalog_consumer){
$this_catalog_items = get_field(‘dl_catalog_items’,$catalogID);
$this_consumer_id = $this_catalog_consumer[0]->ID;
$field = get_field_object(‘dl_interested_in’, $this_consumer_id);
$field_key = $field[‘key’];
$consumer_wish_list_items = get_field(‘dl_interested_in’, $this_catalog_consumer[0]->ID);
$consumer_wish_list_serial = get_field(‘dl_interested_in’, $this_catalog_consumer[0]->ID, false);
// Loop through the wish list items
foreach($consumer_wish_list_items as $wish_list_item):
$wish_list_item_ID = $wish_list_item->ID;
// Loop through the catalog items
// See if there is a match to the wish list item
foreach($this_catalog_items as $catalog_item):
$catalog_item_ID = $catalog_item->ID;
if($wish_list_item_ID == $catalog_item_ID){
// unset this item from the consumer wish list
// Find the array index for this catalog_item_id in the wish list array
$pos = array_search($catalog_item_ID, $consumer_wish_list_serial);
// Remove that position (key and value) from the array
unset($consumer_wish_list_serial[$pos]);
// Now update the Wish List Relationship field value for this consumer
update_field($field_key, $consumer_wish_list_serial, $this_consumer_id);
}
endforeach;
endforeach;
}
return $consumer_wish_list_serial;
}
I hope this is helpful. You can check out my app at gardentracker.com. I LOVE ACF!
You must be logged in to reply to this topic.
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.