Support

Account

Home Forums General Issues Flush Unused Custom Fields

Solved

Flush Unused Custom Fields

    • zaccety

    • September 26, 2018 at 9:47 pm

    +1

    • horace

    • January 20, 2019 at 1:09 am

    recently i also came across this problem. i don’t mind the unused data per se but that it turns up in search results is a bummer. a lot of wordpress sites seem to suffer from this since i find a lot of weird search results on various wordpress sites i experimented with. ๐Ÿ™‚

    what is the current status of this and the cleanup plugins that some users did?

    wouldn’t a simple solution be to just delete all custom fields of a post on save_post (priority 1) and acf will store the currently used ones again afterwards anyway? a batch job could walk through all posts and resave them.

    someone mentioned a potential issue with clone fields but i never used them and don’t even know what they are. ๐Ÿ™‚

  • This is a pretty heavy-handed ‘solution’ but, for what it’s worth, what I’m doing at the moment to remove unused ACF post_meta data is thisโ€ฆ

    I name all my ACF fields with a unique prefix (eg. my_prefix_first_name, my_prefix_last_name, etc…) making sure that my_prefix is unlikely to conflict with any default WordPress post_meta keys or any of the post_meta keys created by plugins that I might use.

    Then, every time I update a post, I simply delete ALL post_meta containing my_prefix using the acf/save_post action below:

    function acf_delete_all_post_meta($post_id) {
        global $wpdb;
        $result = $wpdb->query($wpdb->prepare("
            DELETE FROM wp_postmeta
            WHERE meta_key
            LIKE '%my_prefix%'
            AND wp_postmeta.post_id = %s",
            $post_id
        ));
    }
    add_action('acf/save_post', 'acf_delete_all_post_meta', 1);

    With a priority of less than 10, this action will fire BEFORE the actual post data is saved. Thus, when the post data IS saved, it’s saved to a clean slate and only saves post_meta for fields that are currently being used in the post.

    Obviously, this technique will not flush your entire site of unwanted post_meta in one go โ€“ it only acts upon one post at a time and only when you update the post. However, if used during development (which is probably the only time I would use it) it will help tidy-things-up as you go along.

  • How would you modify this function for a multi-site? I do know the Site ID and the site-specific tables in which I want to delete left over acf meta data

Viewing 4 posts - 76 through 79 (of 79 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.