• I tried this code in my search.php
    <?php
    if( is_search() ) :
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    query_posts(“s=$s&paged=$paged&cat=-71,-6,-69,-68,-73,-75,-74”);
    endif;
    ?>

    but it didn’t work….I am using slug on each categories..How can I work on this for it to be functional?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I think your code did not work because of the minus sign in front of the numbers…

    Try this:

    <?php
    if( is_search() ) :
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("s=$s&paged=$paged&cat=71,6,69,68,73,75,74");
    endif;
    ?>
    Moderator bcworkz

    (@bcworkz)

    The $s variable in query_posts() is not defined. You need something like $s = get_query_var('s'); (untested)

    FYI, using query_posts() is discouraged because the original query is discarded and a new one run. Not very efficient. The preferred way to handle this is through the ‘pre_get_posts’ action. The first example in the following article is pretty much what you want, except use $query->is_search() instead of $query->is_home() . Plugin API/Action Reference/pre get posts

    ah yes, silly me. I totally forgot to mention that the variable was not defined. I have been doing this for too long to gloss over that!

    I think part of the problem is also that the numbers are negative numbers…

    Thread Starter mae48

    (@mae48)

    add_action( ‘after_setup_theme’, ‘dazzling_setup’ );
    function exclude_category( $query ) {
    if ( $query->is_search() && $query->is_main_query() ) {
    $query->set( ‘cat’, ‘71,6,69,68,73,75,74’ );
    }
    }
    add_action( ‘pre_get_posts’, ‘exclude_category’ );

    is this the correct setup??

    Thread Starter mae48

    (@mae48)

    oops, let me rephrase it. How can I search only for simple products?

    Joey

    (@leglesslizard)

    Please use the code tags 🙂 makes it much much easier to read your posts.

    I think your setup is correct, currently altering the search query to only look at those categories (I assume that’s what you were after?)

    I think products in woocommerce are assigned post types depending on types so a product with post type “product” (as opposed to “product_variation” etc) is a simple product. In that case you should just be able to add:

    $query->set('post_type', 'product');

    Regards 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘limit search for specific category’ is closed to new replies.