Include category and post tag names in the WordPress search

It has been a month since my last post.  That is due to the fact that three weeks ago I became a father for the first time.  I just have not had the time to sit down and gather my development thoughts.  Things have settled down a bit and I decided to dig into the WordPress search query.

More specifically, I want the WordPress search to extend to category and post tag names, which comes down to searching on the taxonomy name.  Lucky for me, WordPress has filters to extend the search query.

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.

Feel free to checkout the Github version of this code as a plugin.

Comments

  1. says

    youre code works…

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

    BUT…

    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

      Chris,

      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?

    • admin says

      Jigar,

      Unfortunately, I have no experience with multi-site. So I am not sure where the differences would lie.

  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.

    Thanks,
    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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>