skip to Main Content

I have a woocommerce store and I try to select from Mysql all products and set them as "Featured" products or set them as Non "Featured" products.

I have found the following query that set post_status to whatever I want. I quess that I just have to replace post_status to something else that set products as "featured" products or as no "featured" product.

UPDATE wp_posts 
SET post_status = 'Published' 
AND post_type = 'product'
OR post_type = 'product_variation'

2

Answers


  1. Is there a reason you want to hack this into the MySQL database directly? The proper approach would be to use the WC API in order to do so.

    An example that uses an array of WC products:

    foreach ( $products as $product ) {
        $productObj = $product->get_product();
        $productObj->set_featured(true);
        $productObj->save();
    }
    
    Login or Signup to reply.
  2. You can use following custom PHP code:

    <?php
    error_reporting(E_ALL); 
    define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
    require( './wp-load.php' );
         //echo $end_datenew;
         global $wpdb;
         global $woocommerce;
    
    
    // The SQL query
    $results = $wpdb->get_results( "
        SELECT  p.ID as product_id
        FROM {$wpdb->prefix}posts as p
        
        WHERE p.post_type LIKE 'product'
        AND p.post_status LIKE 'publish'
        AND p.post_parent = '0'
    " );
    
    
    foreach($results as $result)
    {
        $wpdb->insert('h3wp_term_relationships', array(
        'object_id' => $result->product_id,
        'term_taxonomy_id' => '20',
        'term_order' => '0', // ... and so on
    ));
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search