Include Category and Tags in the WordPress Search

Include category and post tag names in the WordPress search

By default, a search in WordPress will only search a post’s title and content.  Through filters, it can be extended to include taxonomies, such as categories and tags.  This does not have to just be tags and categories, custom taxonomies can also be included within the search.

Send in the filters

Use posts_join filter to perform an INNER JOIN of the terms, term_relationship, and term_taxonomy tables on the posts table.

To include a clause to search the text being search, the posts_where filter is available to append an extra where clause.

The included taxonomy names can be narrowed down to category and post_tags.  Wildcards are appended on both sides of the search criteria against the term name.

Finally, the posts_groupby filter is used to aggregate all the post IDs that will fall into out query to match the where clause.

The results may vary

You may not get what you expect.  Categories and tags are usually single words.  If a search contains more than one word, most categories and tags will be skipped.

This could be avoided by chunking the search words and creating a clause for each chunk against the query.  This method could produce more overhead and probably not recommended for larger database.

It should also be noted that including a wild card search on category and tag names isn’t exactly appropriate in most cases.  This would best be used with custom taxonomies such as actors for a movie database.

View the source code on Github


  1. says

    youre code works…

    when searching: yes it will include tags as search terms…


    it will break searchresultspage if searchterm is shorten then 4 chars. when searching for soething shorten then 4 chars.. searchresultst will be echo’ed in the menu..

    any ideas why?

  2. Chris says

    Is there any chance that this can also search 2 or more words? Example I have a post tagged Simple, Clean – so if I input simple clean it will display any post with Simple and Clean tags… or even combined with a category

    • says


      The way the WordPress search functions is by appending a wildcard on each side. So by searching ‘simple clean’, part of the WHERE clause would look like %simple clean%. So the search would be looking for a match that contains ‘simple clean’.

      Off the top of my head, I can think of two ways around this issue. Use mysql full-text search, which would require altering the database engine. I am not sure what side effects this would produce, but the speed and accuracy would be pretty good.

      Second, you could split the string by an empty space and create multiple wild card searches for each word in the string. The performance would probably be decent with a smaller site, but a large site would probably be slow.

  3. Bert says

    Hi Chris, thank you for the plugin!
    Just one thing. I got a warning, that $wpdb->escape is outdated on WordPress 3.8. I had to replace it with esc_sql. With this change it is working very well.

  4. Jigar says

    Hi Chris, thank you for the plugin!

    Just one thing, I want same functionality for WP multisite network. Can you please help me in this?

  5. says

    Hi Ryan
    Thanks for this beautiful piece of code.

    I’m currently building a site where I use custom post types to create products and their categories as implemented here. I was wondering if there is a way to search in the categories of the products as well.

    Nick Pasadakis

  6. Linus says

    This is not your fault, or problem, but when i use the plugin “Post Tags and Categories for Pages” (which just adds the functionality to add categories to pages) i have found that pages must have a category to be found. This does not apply for posts. That is, if I create a page and name it “My awesome test page” and search for “awesome” without the code above, i can find it. If i use the code above and this page has no categories, it will not be found. But, if i give the page a category (does not matter which one) it will be found. Any suggestions for making this work without giving the page a category?

  7. Harsh says

    Thanks, Ryan
    It works awesome.
    Ryan i want to ask that can you give me a code to include custom fields in the search query.
    I am using this arguments for search $args = array(
    ‘post_type’ => array($resource_type,$news_type,$articles_type),
    ‘s’ => $_POST[‘resource’],
    ‘post_status’ =>’publish’,
    ‘posts_per_page’ => -1
    and your custom_posts_join and custom_posts_where functions.
    Please reply fast

  8. hedi says

    hi, nice snippet!
    just a little problem: the search result with this code include posts that are not published. post that are in the draft. something to add to avoid that ?

    • says


      I updated to code to reflect the post status and author status (if you are logged in) when the sql analyzes the taxonomy coniditon.

      This will exclude drafts, but include private posts if the author is logged in.

  9. Dev says

    Hi Ryan,
    I am actually new to wordPress so I don’t have that much idea, where shall I put this codes, in search.php of my theme or query.php.

    Please help

  10. Dev says

    continuing from the above comment

    This is the code i am using to search ….

    $args = array(
        's'         => $_GET['s'],
        'showposts' => -1,
        'post_type' => $searchable_types,// define searchable Post Types
    $tp_allsearch = new WP_Query($args);

    So how can I use your functions with the above queries…

    Thanks and Regards

    • says


      The functionality I provided above are WordPress action and filter callbacks. They will automatically hook into WP_Query that is a search AND the main query. You would have to remove the check for is_main_query() to allow a custom WP_Query to get through.

  11. Dev says

    Your Plugin worked for ‘Tag’ values……..Thanks a lot.
    I have some meta field provided by a Plugin inside ‘create a Post’. Can I include those Meta field as well in my search.
    Regards and once again ….Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *