skip to Main Content

I have the following snippet to check if an item is out of stock and if so to change its status to draft from published. How can I exlude one category in this query? Thank you!

add_action('admin_init', 'woo_moveoutofstocktodraft');

function woo_moveoutofstocktodraft() {
    $current_user = wp_get_current_user();
    //if loggedin user does not have admin previlage
    if (!user_can($current_user, 'administrator')) {
        return;
    }
    $params = [
        'posts_per_page' => -1,
        'post_type' => 'product',
        'post_status' => 'publish'
    ];
    $wc_query = new WP_Query($params);
    if ($wc_query->have_posts()) :
        while ($wc_query->have_posts()) :
            $wc_query->the_post();
            $product_id = get_the_ID();
            $product = wc_get_product($product_id);
            //if product is Out of Stock move to draft              
  if (!$product->is_in_stock()) {
            $post = array( 'ID' => $product_id, 'post_status' => 'draft' );
            wp_update_post($post);
            }
        endwhile;
        wp_reset_postdata();
    endif;
}

2

Answers


  1. use tax_query parameter in your query arg,

    e.i.

    $params = [
        'posts_per_page' => -1,
        'post_type' => 'product',
        'post_status' => 'publish'
        'tax_query' => [
            [
                'taxonomy' => 'product_cat',
                'terms'     => [1,2,3,4], //IDs of categories
                'operator'  => 'NOT IN'
            ]
        ],
    ];
    

    But I’d say this uses way too much resources and doing a lot of useless overhead just for doing a simple thing.

    your code performs the following;

    1. Perform database query to read records on post table
    2. Loop through each record
    3. Another database query (read) for checking stock for each record on meta table (N+1 problem)
    4. Then Another database query (write) for each record meeting specific condition to update a single posts table field.

    you better of doing ONE raw sql UPDATE statement that joins the meta and taxonomy related table, would probably use 99% less resource than your current approach

    Login or Signup to reply.
  2. add_action('admin_init', 'woo_moveoutofstocktodraft');
    
    function woo_moveoutofstocktodraft() {
        $current_user = wp_get_current_user();
        // Check if the current user is not an administrator
        if (!user_can($current_user, 'administrator')) {
            return;
        }
        
        // Define the category ID to exclude
        $excluded_category_id = 123; // Replace 123 with the ID of the category you want to exclude
        
        $params = array(
            'posts_per_page' => -1,
            'post_type' => 'product',
            'post_status' => 'publish',
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'id',
                    'terms' => $excluded_category_id,
                    'operator' => 'NOT IN', // Exclude products from the specified category
                ),
            ),
        );
    
        $wc_query = new WP_Query($params);
        if ($wc_query->have_posts()) :
            while ($wc_query->have_posts()) :
                $wc_query->the_post();
                $product_id = get_the_ID();
                $product = wc_get_product($product_id);
                // Check if the product is out of stock and move it to draft status
                if (!$product->is_in_stock()) {
                    $post = array('ID' => $product_id, 'post_status' => 'draft');
                    wp_update_post($post);
                }
            endwhile;
            wp_reset_postdata();
        endif;
    }
    

    I have modified your code and in this modified code:

    Replace 123 with the actual ID of the category you want to exclude.
    The tax query parameter is added to the $params array to exclude products from the specified category.
    The operator ‘NOT IN’ is used to exclude products from the specified category.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search