Forums

Limit Search to Specific Categories (10 posts)

  1. mbrumley
    Member
    Posted 3 years ago #

    How can I limit the basic search functionality to only echo results from specific categories? There's got to be a way. I want to code it instead of using a plugin because I am having 2 different search forms: one searches through specific categories, the other searches through different categories.

    Thanks!

  2. flick
    Member
    Posted 3 years ago #

    This thread might be helpful. I haven't tried out the plugin myself, but the demo looks fantastic.

  3. mbrumley
    Member
    Posted 3 years ago #

    Thanks...that's not exactly what I am looking for but it's cool.

  4. flick
    Member
    Posted 3 years ago #

    Maybe there's the potential to modify the plugin. It might also be an idea to email the plugin author as well :)

  5. mbrumley
    Member
    Posted 3 years ago #

    I found this which is close...not sure how to implement this.

    wordpress.org/support/topic/145593?replies=3

  6. flick
    Member
    Posted 3 years ago #

    Try leaving out the minus sign when including the categories, and see if that limits the search to those categories, rather than exclude them.

    So:

    <?php
    if( is_search() )  :
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("s=$s&paged=$paged&cat=1,2,3");
    endif;
    ?>
  7. mbrumley
    Member
    Posted 3 years ago #

    Thank you for the help. I think this will do the trick!

  8. flick
    Member
    Posted 3 years ago #

    You're welcome! :)Thanks for the feedback! It's always good to know when a suggestion works

  9. uppaway
    Member
    Posted 3 years ago #

    I don't know where this code is placed - could you elaborate on the placement of this code and anything else I'd need to know to create a search result on a specific category?

    Thanks so much.

  10. xphilippe
    Member
    Posted 3 years ago #

    Hi,
    I was exactly looking for a piece of code to limite the search to certain categories. Your code works well !!

    <?php if( is_search() )  :
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("s=$s&paged=$paged&cat=1,2,3");
    endif; ?>

    But, I want the category to be dynamic :
    From the web page archive.php, I want to store the current category (something like $my_category = "current categorie") and call this variable in the search result web page search.php (query_posts("S= ..." cat=".$my_category). As I am a dummy in PHP langage, I don't know what I need to write .... could you help me?

    Thanks in advance

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags