Home › Forums › Backend Issues (wp-admin) › Delete all related post_object fields when post is deleted › Reply To: Delete all related post_object fields when post is deleted
There is nothing built into ACF that will do this for you and I don’t know of any examples of it being done. The problem is finding the fields with the values and correctly updating the repeater in a way that will not break the repeater.
I would create an acf/load_value filter for the repeater https://www.advancedcustomfields.com/resources/acf-load_value/
add_filter('acf/load_value/name=REPEATER_NAME', 'remove_invalid_posts', 20, 3);
function remove_invalid_posts($value, $post_id, $field) {
if (empty($value)) {
return $value;
}
foreach ($value as $index => $row) {
// your need to use field keys here I think, but you'll need to test
$related_post = $rows['field_1234567']; // field key of relationship field
$post = get_post($related_post);
// I am just seeing if a post is returned here
// you could be more thorough,
// for example testing to see it it's in the trash or whatever if it exists
if (!$post) {
unset($value[$index];
}
}
return $value;
}
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.