Bulk Update Custom Fields for Custom Post Types

Why not save yourself a whole lot of hassle and add a handy admin menu item “Bulk Update” that you can just click whenever you need it.

This method (using hooks) also ensures that WordPress (as well as ACF and your custom post types) are safely loaded and ready to go.

/**
 * Register "Bulk Update" menu item under the "mycptype" admin menu.
 *
 * Menu item links directly to admin-post.php?action=mycptype_bulk_update
 *
 * ...which is handled by {@see wpse_382892_bulk_update()}
 *
 * @see https://developer.wordpress.org/reference/functions/add_submenu_page/
 */
function wpse_382892_menu_item() {
    add_submenu_page(
        'edit.php?post_type=mycptype',
        'Bulk Update',
        'Bulk Update',
        'edit_others_posts',
        // https://developer.wordpress.org/reference/files/wp-admin/admin-post.php/
        admin_url( 'admin-post.php?action=mycptype_bulk_update' ),
    );
}

add_action( 'admin_menu', 'wpse_382892_menu_item' );

/**
 * Handle the bulk update request.
 *
 * @see wpse_382892_menu_item()
 */
function wpse_382892_bulk_update() {
    if ( ! current_user_can( 'edit_others_posts' ) ) {
        wp_die( 'You do not have permission to do this.' );
    }

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

    $the_query = new WP_Query( $args );

    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        $testa = get_field('br_type');
        $testb = get_field('br_category');

        if ($testa === 'Apples') {
            update_field('br_featured', '0', get_the_ID());
        }
    }

    wp_die( 'Bulk update completed!', 'Bulk Update', [
        'back_link' => true,
    ]);
}

// This custom action is fired in wp-admin/admin-post.php
add_action( 'admin_post_mycptype_bulk_update', 'wpse_382892_bulk_update' );

Note that you don’t actually need the menu item for the second function to still be usuable – you can trigger the action manually just by loading the action URL in your browser:

https://example.com/wp-admin/admin-post.php?action=mycptype_bulk_update