Home Forums Backend Issues (wp-admin) Delete all related post_object fields when post is deleted


Delete all related post_object fields when post is deleted

    • adamk22

    • September 18, 2019 at 9:06 pm

    I’ve made a repeater that includes rows of post object fields that reference a custom post type. Whenever a user deletes a post, all the repeaters (in other posts or pages) that has a row referencing to that deleted post remain visible but with an empty value. Is there a common technique to scan the repeaters or get all acf post object/relationship fields referencing a deleted post and remove said row?

    I was thinking of making a custom implementation using the wpdb class, but I am hoping for a hook or filter that I can use. I wasn’t able to find any related to my issue in the docs.

  • 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

    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) {
      return $value;
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Delete all related post_object fields when post is deleted’ is closed to new replies.

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.