Support

Account

Home Forums General Issues ACF field not deleted on page save

Solved

ACF field not deleted on page save

  • Hi,

    First of all, thanks for continuing to maintain and improve this incredibly useful plugin.

    I use an ACF select field called ‘gc_nav_menu’ on top-level pages that lets editors select a custom menu for these pages, defaulting to the primary navigation menu. Subpages then inherit the navigation menu of their closest ancestor by looking up the ‘gc_nav_menu’ field on their parent, then grandparent page, etc.

    Thus, when I create a new page and publish it, it gets assigned a ‘gc_nav_menu’ of ‘primary’. If I then decide to the make this page a subpage, the gc_nav_menu ACF meta box not longer shows up on the page editor, but the page still retains it’s original ‘gc_nav_menu’=’primary’ value, instead of NULL as I’d expect.

    This screws up the ability of subpages to inherit their ancestor page’s menus, and is completely opaque to users b/c there’s no indicator on the page editor that this subpage has a custom menu assigned to it. Furthermore, it’s not clear that there’s any way of removing the gc_nav_menu field value, even for site administrators.

    In fact, I can delete the entire gc_nav_menu Field Group, and pages with this field will still hold onto their custom field value.

    This is problematic for the long-term maintenance and usability of my site b/c it means that over time, with template development and theme changes, my pages will gain more and more ACF field values that will not show up on the page editor but will influence template logic in unexpected ways. Am I missing something, or is a solution to this problem in the works?

    Thanks for your help,

    – Micah
    Web Developer @ Goshen College

  • This question actually comes up quite often.

    ACF has never removed values that are no longer used and it has always been the case that we must implement our own logic to decide if we should get a field or not get a field. The same is true of conditional logic. Let’s say that you have a true false field and two fields based on if this field is checked or not. In your template you must check this field to see if it’s true or false because both of the fields might contain a value.

    I your case, since sub pages will not have this menu field, the first thing you should be checking is if the page is a child of another page.

    
    if ($post->parent != 0) {
      // this is a child page.
    }
    

    The issue is a deep one, ACF does not know when saving a post, what fields are included and what fields are not. Really the point is that ACF does not know, and cannot figure out what fields exist that might need to be deleted or removed. There isn’t a way to get a list of all field and groups that ARE NOT included for a location. The only thing it has to go by is what is submitted and what fields ARE included for a location.

    If it is necessary to make sure that values are removed when no longer needed then what we need to do is create an acf/save_post filter and do this checking ourselves and delete what we don’t want any more.

    This is the same way the WP works. If you created your own custom fields not using ACF, then you removed the meta boxes from the admin interface for those fields all of that data would still exist in the database. If you use a particular theme that creates custom fields and then switch themes, more than likely any content in those custom fields will still exist if you decide to switch themes.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘ACF field not deleted on page save’ is closed to new replies.