Support

Account

Home Forums Backend Issues (wp-admin) How do I clean up old ACF fields in DB?

Solving

How do I clean up old ACF fields in DB?

  • Hi,

    I’m using Yoast to do our SEO. I pull all the custom fields with following code, and then pass the values to Yoast.

           global $post;
             $pid = $post->ID;
    
             $custom = get_post_custom($pid);

    Problem is, I notice that ACF has saved all of my fields and the positions of those fields, including ones that no longer exist. So I had a field named h2 in the flexible field pageblock that used to be first position, so the name (key) is pageblock_0_h2. Then I moved down to position 3, then ACF created pageblock_2_h2, but left pageblock_0_h2 intact.

    The issue is that Yoast is analyzing a page with more content on it than actual. For example, Yoast sees 5 images instead of the actual 3.

    How can I automatically clean up those old DB entries? Thanks!

  • I’m not entirely sure this would work, however, if someone is adventurous and has some time to kill they could. This is just a general outline.

    Create an acf/save_post function.

    In this function get all of the postmeta fields in the database for the post. You’d want to specifically get all fields with a value like ‘field_%’. The meta_key values of these fields in the database are you field name pre_pended with an underscore.

    You could then loop through all of the posted data that is posted using the acf field key values (‘field_%’) and compare them to the list of fields that you collected in the first step, removing from your list anything that’s being submitted. This would be the hard part because the posted data will be a complex nested array.

    What you should have left is a list of fields that are no longer used.

    loop trough the this list and delete all of the unused fields ‘MY_FIELD’ & ‘_MY_FIELD’ using delete_post_met();

    If you could get it to work it might be a useful for others.

    • Dalton

    • March 9, 2017 at 4:04 am

    I’m going back to several old threads to report the fix for this problem. As of ACF Pro 5.5.8, flexible fields clean up after themselves when they’re deleted and no longer leave orphan data in the postmeta table.

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

You must be logged in to reply to this topic.

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.