Caching Issue

Hi there,

I am using php to change an ACF true/false field of posts, for bulk post changes.

// Define the post type
$post_type = 'my_special_post_type';

// Get all posts of the specified post type
$args = array(
    'post_type' => $post_type,
    'posts_per_page' => -1,
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        // Get the post ID
        $post_id = get_the_ID();

        // Update the ACF field 'post_status' for each post
        update_field('post_status', true, $post_id);
    }

    // Reset post data
    wp_reset_postdata();
}

The problem I am facing is that the value is displayed (true=‘1’, false =‘’) correctly in the frontend, but I am not able to set the Cwicly conditioning (if true display it). Only when I go to the post where the value was changed, don’t change any field and just manually update the post, the cwicly conditioning works again by showing the 1 of that single post.

I think it might be some kind of caching issue, but I don’t know how to check.

I’m not sure where the problem originates, wordpress, ACF or Cwicly, and would appreciate any advice or ideas.

Cheers

Try this:
update_field('post_status', 1, $post_id);

And If the post does not already contain a value, try to use the field_key instead of the field name.

Hi @krievinshj,

thank you for the reply.

Do you mean something like this?

// Define the post type
$post_type = 'my_special_post_type';

// Get all posts of the specified post type
$args = array(
    'post_type' => $post_type,
    'posts_per_page' => -1,
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        // Get the post ID
        $post_id = get_the_ID();

        $field_name = 'post_status';

        // Get the existing field value
        $current_value = get_field($field_key, $post_id);

        $my_value = true;

        // If field already contains a value
        if ($current_value !== null) {
            update_field($field_name, $my_value, $post_id);
        } else {
            // If the post doesn't contain a value, use the field key directly

            // Get the field information using the ACF field name
            $field_key = acf_get_field($field_name)['key'];

            update_field($field_key, $my_value, $post_id, true);
        }
    }

    // Reset post data
    wp_reset_postdata();
}

The error was on my end, I had a typo in the meta key because I also filtered with a query :face_exhaling:

Always these user errors…

Great if it worked!
Yes, those typo errors, quietly stealing our hours. :grinning:

1 Like