skip to Main Content

I have a meta attribute on my products that can be accessed like this:

get_post_meta( $product_id, '_disable_shipping', true );

I need to update _disable_shipping attribute of all products on MySQL or programmatically. Is there a simple way of doing it? I want to change all values to yes for all my products.

2

Answers


  1. You can create a plugin and put a function in it which runs through your products and updates every products meta value. If you don’t know how you create a plugin, here is a tutorial for this: https://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/ It is done in minutes.

    Please make sure you have a backup of your database before doing bulk actions to update your products.

    The function could look something like this:

    function disable_shipping_once(){
    
    $products = get_posts(array('post_type' => 'product', 'numberposts' => -1) );
    
    foreach($products as $product) :  
    
    
        $product_ID = $product->ID;
        $meta_value = get_post_meta($product_ID, '_disable_shipping',true);
    
        if($meta_value == "no") :
    
            update_post_meta($product_ID, '_disable_shipping-include', 'yes' );
    
        endif;
    
    endforeach; 
    }
    

    If you activate your plugin, this function will run. After that, you can deactivate it again.

    This code should be pretty save, because it only updates post meta, if the value is “no”, so there should not be any unintended updates of the posts.


    Maybe there also is a plugin for bulk editing your products. This is the first I found which seems to be doing the job, haven’t tested it yet: https://wordpress.org/plugins/custom-field-bulk-editor/

    Login or Signup to reply.
  2. You can use the following MYSQL query , wp_postmeta depends on your wordpress database prefix

    UPDATE `wp_postmeta` SET `meta_value` = 'yes' WHERE `meta_key` = '_disable_shipping'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search