Bulk Update Custom Fields for Custom Post Types
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:
- Insert the code inside your functions.php and refresh your website ONCE
- 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?
Topic advanced-custom-fields bulk custom-field custom-post-types Wordpress
Category Web