Essentially what I need to do is create a “default” value for a custom meta box.

the number in the meta box will be constantly changing, but in order for my code to work, I need to start with a value of 1 in the field.

I need to update this for all woocommerce products, and the meta_field is called search_priority

I started with some php as I prefer to operate outside of the sql database.

add_action('init', 'update_all_products_to_default');
function update_all_products_to_default() {
  $args = array(
    'posts_per_page'   => -1,
    'post_type' => 'product'
  );
  $posts_array = get_posts( $args );

  foreach($posts_array as $post_array) {
    update_post_meta($post_array->ID, 'search_priority', '1');
  }
}

now, I really think that this would work, but my site ( only tried on mamp so far ) kept crashing.

when I changed posts_per_page to 1000, the code ran and updated the first 1000 products on my site. great … except I have 26,000 total, is there any way to add $paged to this array ? and maybe I can run this code one page at a time?

So now I am turning to SQL in attempts to enter in the value 1 for each product.

here is where I am at.

UPDATE wp_postmeta 
SET meta_value= 1 
WHERE meta_key = 'search_priority' 
AND post_id IN (SELECT ID FROM wp_posts WHERE post_type = 'product')

however no luck here yet.

I would really appreciate any pointers.

Thank you

Read more here: SQL query to update meta_value for one meta_key for all post_type = product


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: