2

I have pages, posts and woocommerce product categories and products in my website. I want to limit wordpress default search query so that it returns the posts, the pages, the product categories but NOT individual products. I am using the following code in my functions.php with which I can easily show only posts and pages. What I need now, is to show the woocommerce product categories along with my posts and pages in the search result, but NOT the individual products. Please help me out here.

function searchfilter($query) {

if ($query->is_search && !is_admin() ) {
    $query->set('post_type',array('post','page'));
}

return $query;
}

add_filter('pre_get_posts','searchfilter');
1
  • I got that wrong.
    – Yamu
    Commented Apr 14, 2016 at 11:41

2 Answers 2

3

You may need to include a tax_query for the Woocommerce taxonomy (called 'product_cat'):

    $tax_query = array(
        array(
            'taxonomy' => 'product_cat'
        ),
    );
    $query->set( 'tax_query', $tax_query );   
}

return $query;
}

However, you'll have to ensure that you can return posts AND pages AND product categories and also note the search results will be mixed up together.

I would have thought a better solution, rather than filtering at functions.php level, would be to adapt your search.php for the display of the search results. You can then be quite targeted, for instance show:

Posts with this search include:
PostX, PostY, PostZ.

And then another loop with:

Pages with this search include:
PageA, PageB, PageC.

And then another loop with:

Product Categories with this search include:
Product Cat A, Product Cat F, Product Cat Z.

Is that the kind of result you are after, or would you like to filter EVERY search on your site and mix up the results?

7
  • 1
    Thanks @Monkey Puzzle :) .. I will try your solution tomorrow and get back to you :)...cheers
    – Saif Islam
    Commented Apr 14, 2016 at 16:56
  • ahh..mixing up things will create problem I think...can you please elaborate how do I implement your second solution in my search.php file ? currently there is only one while loop in that file...however I will also implement your first solution just for testing purpose...you modified your code above..i will include your code into mine and check whether its feasible for me or not...thanks anyway...
    – Saif Islam
    Commented Apr 15, 2016 at 10:24
  • btw, your first solution doesn't work :(
    – Saif Islam
    Commented Apr 15, 2016 at 11:48
  • 1
    Yeah, I realised that would have filtered out everything to ONLY show product_cats, and of course you can't have posts which are product_cats, so it won't show anything. Sorry about that. You'd need to reword it so it shows pages AND posts AND product_cats, but I don't think you could easily separate them and the results would be all mixed up. Commented Apr 15, 2016 at 11:51
  • 2
    To get multiple loops on the search results page, you could follow a tutorial like this one. You end up getting a very nice clean display of results which you can format any way you like. Commented Apr 15, 2016 at 11:57
0
add_action('pre_get_posts','search_filter_exc_posts');
function search_filter_exc_posts($query) {
    // Verify that we are on the search page & this came from the search form
    if($query->query_vars['s'] != '' && is_search())
    {
        $q_tax_query = $query->query_vars["tax_query"];
        // append product categories to current tax query.
        $query->set('tax_query', $q_tax_query[]=array('taxonomy'=>'product_cat') );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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