0
votes

I have a WordPress website which uses ACF (Advanced Custom Fields). I have 2 custom select fields which allow a user to be selected on posts - these fields are called 'Author' and 'Editor'.

The site has around 5000 posts and, for some of the older posts on the site, the custom 'Author' and 'Editor' fields are not being pulled through to the frontend of the site even though the custom fields appear to be set correctly within the post editor screen.

If I go in to an individual post and re-save/update it, then the custom fields appear to then work fine and are pulled through to the frontend of the site. Unfortunately, bulk editing the posts and re-saving/updating them doesn't appear to have the same effect of fixing the posts. As I have 5000 posts it's not really viable to re-save/update each one individually.

Update: I had already tried the instructions in the link provided in the comments from Stender - it doesn't appear to update the ACF fields data in the same way as manually updating a single post. Does something different happen during the manual method as opposed to using wp_update_post()?

Does anyone have any ideas on how I can fix the posts where the custom fields aren't being pulled through to the frontend in bulk?

If you need any more information or have any questions, please feel free to ask.

2
I guess you can follow the answer on this question, since it updates/resaves the posts : stackoverflow.com/questions/7917725/update-all-wordpress-posts - Stender
Hi Stender, thanks for your comment. Unfortunately, I have tried this already and although it updates the posts it doesn't seem to save/update the custom fields... - Mat

2 Answers

1
votes

So, it turns out that the ACF custom field data is actually stored correctly already, but for some reason the ACF function get_field() isn't pulling in the data for certain posts when it is definitely there. It appears to be old posts that are affected only and I'm not sure of the exact cause. (Older posts may have been imported, but I'm not 100% sure)

Anyway, I got around the problem in the end by using WordPress' own function for retrieving meta data/custom fields: get_post_meta(). There's a bit more code involved but it now works for me.

Thanks for everyone's efforts in helping anyway!

0
votes

I found a blog post with instructions on how to bulk-update ACF (post_meta) fields.

function mass_update_posts() {

    $args = array(
        'post_type'=>'post-type',
        'posts_per_page'   => -1
    );

    $my_posts = get_posts($args);

    foreach($my_posts as $key => $my_post) {
        $meta_values = get_post_meta( $my_post->ID);

        foreach($meta_values as $meta_key => $meta_value) {
            update_field($meta_key, $meta_value[0], $my_post->ID);
        }
    }
}

You can call this function by hooking into init.

add_action( 'init', 'mass_update_posts' );

I would add the action and function, load the website once, and then comment it out so it doesn't load again.