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βre excited to announce we've released Composer support for installing ACF PRO.
— Advanced Custom Fields (@wp_acf) January 31, 2023
π #ComposerPHP fans rejoice!
β¨ Please see the release post for all the details and full instructions. https://t.co/ebEfp61nlR
© 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.