Ecommerce Shopify WordPress Discussion

How to exclude a product category from query in functions.php WordPress?

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; }
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], //ID\s 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; Perform database query to read records on post table Loop through each record Another database query (read) for checking stock for each record on meta table (N+1 problem) 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

February 13, 2024

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.

February 13, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.