2

I am using this function which works great in case anyone wants to bulk update custom fields for custom post types (just change parameters as necessary).

 $args=array(
  'posts_per_page' => -1,
  'post_type' => 'mycptype'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
  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());
    }
  endwhile;
  wp_reset_postdata();
endif;

How to use:

  1. Insert the code inside your functions.php and refresh your website ONCE
  2. Then don't forget to delete the code.

The problem I am having is that even if the function works as expected and updates the value of the custom field, I need to go inside each custom post type and click on the button "Update" for the changes to take effect.

This is because the ACF field has been registered after the custom post types were published.

Is there a workaround for not going inside each post and click update?

1
  • 1
    Instead of update_field use update_post_meta May 29, 2022 at 13:16

2 Answers 2

1

When you access get_field() from functions.php it will load before ACF has initialized. You can solve it by wrapping the code inside acf/init hook. Update the code as follows.

Hope it helps @JoaMika

add_action('acf/init', 'custom_code');

function custom_code() {
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'mycptype'
    );
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) :
        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());
            }
        endwhile;
        wp_reset_postdata();
    endif;
}
0

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

1
  • this is interesting. How could we use this to bulk update Author for example? Apr 21, 2023 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.