X

Add Dropdown to Search by All Categories or Define a Specific Category

Snippets by IsItWP

Do you want to create a search form with dropdown to search all categories? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add dropdown to search by all categories or define a specific category in WordPress.

Instructions:

All you have to do is add this code to your theme’s index.php file or where you want to display the search form:

<form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
  <div>
    <label class="screen-reader-text" for="s">Search for:</label>
    <input type="text" value="" name="s" id="s" />
    in <?php wp_dropdown_categories( 'show_option_all=All Categories' ); ?>
    <input type="submit" id="searchsubmit" value="Search" />
  </div>
</form>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 12 best WordPress plugins for bloggers and how to save partial form data in WordPress.

Comments   Leave a Reply

  1. Thanks! I spend weeks for this solution. I just put this code creating a file searchform.php on my child theme

  2. Not working with custom post typewith custom taxonomy

    1. How would you do it with a custom post type?

  3. stephen brunton October 2, 2018 at 9:14 am

    where would i add this code as this is exactly what im after

    1. Looks like you need to add it on a HTML widget.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!