On certain user interactions, I want the category page to show products from another category and not show the current category's products. For example - If the current category is laptop-bags , I want to remove products with this category and show products having another category sling-bags. I have tried to do so using the following code:

function check_pre_get_posts($wp_query) {
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' => 'sling-bags',
                                        'operator' => 'IN'
add_filter('pre_get_posts', 'check_pre_get_posts');

However, the above code leads to the category showing no products at all. How to do this?

  • 1
    Does it work if you change $wp_query to, say, $my_query? $wp_query is a global variable, and so it might be causing unanticipated issues.
    – Pat J
    Jan 28 at 19:01

1 Answer 1


I was able to solve the issue by adding the following before $wp_query->set(' :

$wp_query->set( 'product_cat', 'sling-bags' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.