If you don’t want a query to run on the admin you can call the conditional function is_admin()
to check, if it returns true you can just return out of the function. Additionally, pre_get_posts
is an action hook.
function exclude_product_cat_children( $wp_query ) {
if( is_admin() ) {
return;
}
if( isset( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query() ) {
$wp_query->set(
'tax_query', array( array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $wp_query->query_vars['product_cat'],
'include_children' => false
) )
);
}
}
add_action('pre_get_posts', 'exclude_product_cat_children');
Related Posts:
- WP_Query: Exclude hidden products from WooCommerce product list
- How to query only for products with status “in stock” in WooCommerce? [closed]
- Query WooCommerce orders where meta data does not exist
- WP_Query for WooCommerce Products
- Changing a theme’s search function to only show results from woocommerce?
- Get WooCommerce best selling products for the current month [closed]
- Woocommerce query by price range and custom meta key
- WooCommerce: filter by parent product’s taxonomy and product variation’s meta data
- Retrieve products with specific attribute and in category – woocommerce
- How to combine tax_query and date_query in WordPress
- how to retrieve specific product attribute value in an sql query?
- Find all product that contain terms with %keywords% in WP_Query
- Display featured products through custom loop in WooCommerce 3 [closed]
- WP Query – Get WooCommerce Products with variation that is in stock
- Search product by brand name in wp_query
- Get product list of given category
- How to exclude products by tag from woocommerce shop page?
- WooCommerce Only OnSale Products Loop Snippet [closed]
- How to get the WC_Product Object when using a WP_Query in WooCommerce
- Extending woocommerce admin product search
- merge two query arguments into one WP_Query call
- Woocommerce returns Product post_status as published even tho it is in status draft
- WC displaying products from category
- showing all search result in one template
- Get all products which have both product category
- Woocommerce query all products and categories
- Take all produts by category
- Sort by price woocommerce by ID post_meta
- How to get all product attributes for filters in WooCommerce?
- Cluster WooCommerce products in each Category Archive by Tags assigned to products
- WP_QUERY to display woocommerce products whose author IS the logged in user
- Extend product search with meta in WooCommerce
- WooCommerce – get product sub categories
- Having trouble with WooCommerce Storefront child theme single.php “related posts” modification [closed]
- Woocommerce – Changing the order of the upsell products [closed]
- Out Of memory issue on post per page parameter
- wp_query not working with post_type
- Woocommerce – exclude only older out-of-stock items
- Need Sub-category with main Category
- Woocommerce pagination and result count after custom loop
- How to query woocommerce product by pricing? [closed]
- How to order WP_Query to group results?
- Can’t fetch Custom Post Type Data through Custom Query
- Redirect Search to Form When No Product Results Are Found
- How do I get Woocommerce product variation name and variation description in a WP_Query?
- WP_Query Taxonomy categories filtering
- Exclude a Woocommerce product from WP_Query
- Hide products in uncategorized category from search results
- Woo Commerce using WP_Query to get products that match price range, with an additional required product attribute
- Multiple meta_key ordering with pre_get_posts
- How to echo woocommerce category name
- Assign a minimum result count for WooCommerce query shortcodes?
- Woocommerce: order posts by meta key
- var_dump of WP_Query object is not empty, while .have_posts() return false
- WP_Query for products always returns empty
- Unlimited Page Sitemap Link
- $wp_query->found_posts not returning correct value
- Tax Query on product_cat using NOT IN as operator does not exclude that category
- Woocommerce Shortcode which displays a table with product data
- Make a products only viewable to a specific user ID in meta_query pre_get_posts
- Customize the Sorting Dropdown in WooCommerce use another list or more page
- ElasticPress is (aparently) messing with my search filters
- Woocommerce Get Orders By Meta Value
- Save large WP_Query to transient === false
- Custom Woocommerce Product Query
- WooCommerce WP_Query using tax_query returns no results (0 = 1)
- How sort products by calculate value? ( custom post meta, price, option)
- wc_get_products() return empty object
- How can I filter products by meta in Woocommerce REST API?
- get_posts return only first result
- What code to use in an array to call the current sub-category?
- Get categories within specific term
- Select multiple categories with is_tax
- How to display Woocommerce products list by tag
- Woocommerce featured products query no longer working
- Ajax buttons not working properly in WooCommerce when using wc_get_template_part
- update_post_meta performance in a loop woocommerce
- update_post_meta performance in a loop woocommerce
- Query child posts with tax query on parents
- Woocomerce – Order products by float attribute in archive pages
- Not able to fetch woocommerce variation sales products for particular category products
- Slow Query with more 100 products
- Create global array to compare inside a query
- Get posts with any value in attribute
- How do i query woocommerce booking post type on init action?
- WP_Query to Retrieve Posts from WooCommerce Products that are Only in Catalog
- How to add in WP_Query to every 3 posts displayed? [duplicate]
- Woocommerce WP_Query post__not_in problem
- Using the same WP_Query for shop and widgets in WooCommerce shop
- Woocommerce orderby rand with tax_query not random
- Adding arguments to WooCommerce Product Loop using AJAX
- Show linked products using wp query and woocommerce
- Filtering product search results using tags
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Variations as Single Products [closed]
- WP_Query (or WC_Product_Query) out of memory
- Multiple WP_Query loops with Pagination Not Working
- WP_Query date_query – Use unix timestamp?
- Filter and Order by Multiple Custom Meta Values
- Redirect logged in users from a custom post type archive page if they don’t have a specific role