June 17, 2011

TEXT SIZE minus plus

How To Display Only Specific Category In Search Results

Advertisement
You Might Be Interested In Our Popular Article
  • Most Essential Free Softwares After Windows Installation
  • There are times when you make some specific category for just review or promotional stuff and would not want to get that included in your search results. This simple tutorial will help you to include only specified categories in your search results, so your users will have more accurate search results.

    Procedure:

    Note: Do take backup of the files before you try this.

    01. Login to wordpress administration panel and go to Posts => Categories

    02. Once there find the category you want to know the ID for and hover over it, you should see edit link. Right click on it and select Copy Link Location (in firefox) and then open a new tab and paste it there. You should see your category ID as tag_ID=xx. That xx is your category id.

    03. Place this code in your functions.php file:

    function searchcategory($query) {
    if ($query->is_search) {
    $query->set('cat','3,7');
    }

    return $query;
    }

    add_filter('pre_get_posts','searchcategory');


    03. Simply change 3 and 7 to the categories you want to use for your search results, you can add as many as you want by separating them with commas (,).

    This concludes this simple and useful tutorial. Hope you like it.



    Print

    About the Author

    When I was born I was so shocked I didn't speak for a year and half...... My brain has 2 parts, Right and Left, The Right one has nothing left in it and the Left one has nothing right in it......Who says Nothing is Impossible?? I've been doing nothing for years......