Add Search by Category Functionality to Your WordPress Site


If you have a site with a lot of content and a number of different categories, then there’s a good chance your visitors will want to search within certain categories and not others. The solution to this problem is to make your site searchable by individual categories.

Below are two possible ways you could do that – one with a plugin and another more manually.

Use Search By Category Plugin

(DOWNLOAD HERE)

This plugin is pretty straight-forward in terms of what it does: it adds a drop-down box to your search function for individual categories. Here’s a look at  it in action.

And a look at the settings page …

 

 

After installing and activating this plugin, you will either need to place the following code in your theme where you would like the search box to show up …

<?php if(function_exists('sbc')){
    sbc();
} else { ?>
    // Your regular form code goes here
<?php } ?>

Or you can place the following shortcode in a post or a page.

[sbc]

This info can also be found on the plugin’s instructions page.

Although this plugin doesn’t come with a widget, you can place your shortcode in a text widget, and then if necessary, add the following line of code to the bottom of your functions.php file in order to allow your text widgets to accept shortcodes.

add_filter( 'widget_text', 'do_shortcode');

 

Manually Expand the Functionality of Your Current Search Box

Another slightly more complicated method is to give your current search box “search by category” functionality. I found this trick at Deluxe Blog Tips.

Place the following code into the searchform.php file of your theme. (Appearance > Editor > Search Form – searchform.php) If your theme does not have a searchform.php file, you can create one:

 

<form id="searchform" method="get" action="<?php bloginfo('url'); ?>">
    <input type="text" name="s" id="s" size="15" />
    <?php wp_dropdown_categories('show_option_none=Select category'); ?>
    <input type="submit" value="Search" />
</form>

Then put this code into the bottom of your functions.php file. (Appearance > Editor > Theme Functions – functions.php):

add_action('pre_get_posts', 'search_by_cat');
function search_by_cat()
{
    global $wp_query;
    if (is_search()) {
        $cat = intval($_GET['cat']);
        $cat = ($cat > 0) ? $cat : '';
        $wp_query->query_vars['cat'] = $cat;
    }
}

And here’s the result:

One nice feature of this method is that when you are on the category page for a specific category, the search box will automatically switch to that category for more searches.

 

Photo: Pretty woman looking through binoculars against blue background from BigStock

12 Responses

  • great tip on the search by category plugin, pity though that of the entire plugin only the capability checking rules are internationalized and the rest simply “forgotten”.

    The manual fix from Deluxe Blog Tips also hasn’t been internationalized, but as that is not released as a plugin and much more for personal use, it will be much easier to adjust the two words only anyways.

    Perhaps a(nother) good resolution for WPMU this year to only recommend plugins that actually have been internationalized?

  • Thank you – Thank you – Thank you

    I have tried every plugin I could find to just be able to search by category and none of them worked quite right. The “search by category” plugin kinda worked, but I couldn’t get it to display correctly (I hadn’t try your code add yet)

    I added your code as suggested and it works perfectly – even better than what I wanted.

    Just so people aren’t afraid – I know pretty much less than zero about coding so you just need to be able to follow directions.

    Thank you for this tip :)

  • Joseph, thanks for the information.

    I’d like to know if there is any way to put the form on the bottom of the page I’m using it. When I use the [sbc] code on a page, the form appears on the top and I’d like to see it on the bottom. I don’t know how can I change it. It seems it’s not a problem of style (I deleted the CSS file and the form appears on the top).

    I hope you can help me. Thanks in advance.
    David.

  • Author

    David – If you’re using the shortcode, it “should” appear where you put it on the page. Not sure why it isn’t. If you’re using it on all your pages, you’ll probably want to insert the code in your theIme template.

    You might contact the theme developer if you continue to have problems.

    Also, I guess there’s the possibility that this is conflicting with a plugin you have running.

  • New Recruit

    I use wordpress 3.42 multisite and buddypress 1.61 with multiple category select widget. The author of the plugin does not respond to the support page. I want see multiple category before header website. So I inserted the following code in header.php after

    `

    Cerca Hotel per 

    » a) Italia – Provincia Italia – Agrigento Italia – Alessandria Italia – Ancona Italia – Aosta Italia – Arezzo Italia – Ascoli Piceno Italia – Asti Italia – Avellino Italia – Bari Italia – Barletta-Andria-Trani Italia – Belluno Italia – Benevento Italia – Bergamo Italia – Biella Italia – Bologna Italia – Bolzano Italia – Brescia Italia – Brindisi Italia – Cagliari Italia – Caltanissetta Italia – Campobasso Italia – Carbonia-Iglesias Italia – Caserta Italia – Catania Italia – Catanzaro Italia – Chieti Italia – Como Italia – Cosenza Italia – Cremona Italia – Crotone Italia – Cuneo Italia – Enna Italia – Fermo Italia – Ferrara Italia – Firenze Italia – Foggia Italia – Forlì-Cesena Italia – Frosinone Italia – Genova Italia – Gorizia Italia – Grosseto Italia – Imperia Italia – Isernia Italia – L’Aquila Italia – La Spezia Italia – Latina Italia – Lecce Italia – Lecco Italia – Livorno Italia – Lodi Italia – Lucca Italia – Macerata Italia – Mantova Italia – Massa-Carrara Italia – Matera Italia – Medio Campidano Italia – Messina Italia – Milano Italia – Modena Italia – Monza e della Brianza Italia – Napoli Italia – Novara Italia – Nuoro Italia – Ogliastra Italia – Olbia-Tempio Italia – Oristano Italia – Padova Italia – Palermo Italia – Parma Italia – Pavia Italia – Perugia Italia – Pesaro e Urbino Italia – Pescara Italia – Piacenza Italia – Pisa Italia – Pistoia Italia – Pordenone Italia – Potenza Italia – Prato Italia – Ragusa Italia – Ravenna Italia – Reggio Calabria Italia – Reggio Emilia Italia – Rieti Italia – Rimini Italia – Roma Italia – Rovigo Italia – Salerno Italia – Sassari Italia – Savona Italia – Siena Italia – Siracusa Italia – Sondrio Italia – Taranto Italia – Teramo Italia – Terni Italia – Torino Italia – Trapani Italia – Trento Italia – Treviso Italia – Trieste Italia – Udine Italia – Varese Italia – Venezia Italia – Verbano-Cusio-Ossola Italia – Vercelli Italia – Verona Italia – Vibo Valentia Italia – Vicenza Italia – Viterbo » a.) Altri Paesi Afghanistan Albania Algeria Andorra Angola Anguilla Antigua e Barbuda Arabia Saudita Argentina Armenia Australia Austria Azerbaijan Bahamas Bahrain Bangladesh Barbados Belgio Belize Benin Bhutan Bielorussia Birmania Bolivia Bosnia ed Erzegovina Botswana Brasile Brunei Bulgaria Burkina Faso Burundi Cambogia Camerun Canada Capo Verde Ciad Cile Cina Cipro Città del Vaticano Colombia Comore Corea del Nord Corea del Sud Costa d’Avorio Costa Rica Croazia Cuba Danimarca Dominica Ecuador Egitto El Salvator Emirati Arabi Eritrea Estonia Etiopia Filippine Finlandia Francia Gabon Gambia Georgia Germania Ghana Giamaica Giappone Gibuti Giordania Grecia Grenada Guatemala Guinea Guinea Bissau Guinea Equatoriale Guyana Haiti Honduras India Indonesia Iran Iraq Irlanda Islanda Isole Fiji Isole Marshall Isole Salomone Israele Kazakistan Kenya Kirghizistan Kiribati Kosovo Kuwait Laos Lesotho Lettonia Libano Liberia Libia Liechtenstein Lituania Lussemburgo Macedonia Madagascar Malawi Maldive Malesia Mali Malta Marocco Mauritania Mauritius Messico Micronesia Monaco Mongolia Montenegro Mozambico Namibia Nauru Nepal Nicaragua Niger Nigeria Norvegia Nuova Zelanda Oman Paesi Bassi Pakistan Palau Panama Papua Nuova Guinea Paraguay Perù Polonia Portogallo Qatar Regno Unito Repubblica Ceca Repubblica Centrafricana Repubblica del Congo Repubblica Dem. del Congo Repubblica Dominicana Repubblica Sudafricana Romania Ruanda Russia Saint Kitts e Nevis Saint Vincent e Grenadine Samoa San Marino Santa Lucia Sao Tomé e Prìncipe Senegal Serbia Seychelles Sierra Leone Singapore Siria Slovacchia Slovenia Somalia Spagna Sri Lanka Sudan Sudan del Sud Suriname Svezia Svizzera Swaziland Tagikistan Taiwan Tanzania Thailandia Timor Est Trinidad e Tobago Tunisia Turkmenistan Tuvulu Ucraina Uganda Ungheria Uruguay USA Uzbekistan Vanuatu Venezuela Vietnam Yemen Zambia Zimbabwe » b) Tipologia Affittacamere Bed and Breakfast Casa di campagna Hotel Locanda » c) Stelle 1 stella 2 stelle 3 stelle 4 stelle 5 stelle Nessuna stella » d) Servizi Centro benessere Internet Parcheggio Piscina Ristorante Wi-fi
    `

    The multi category search open the following link:
    http://www.example.org/categories/16306,8128/search_type/order/

    But I want this link:
    http://www.example.org/categories/16334,8129/search_type/and/order/title/

    Can you help me?

Comments are closed.