I'm trying to create a search form/filter that allows visitors to search by specific tags.

I've implemented this solution:

<form method="get" action="<?php bloginfo('url'); ?>">

<input type="text" name="s" value="" placeholder="search…" maxlength="50" required="required" />

<p>Refine search to posts containing chosen tags:</p>

// generate list of tags

$tags = get_tags();
foreach ($tags as $tag) {


    '<input type="checkbox" name="taglist[]" value="',  $tag->slug, '" /> ',


<button type="submit">Search</button>

And this is in functions.php to make it work:

// advanced search functionality
function advanced_search_query($query) {if($query->is_search()) {

// tag search

if (isset($_GET['taglist']) && is_array($_GET['taglist'])) {
$query->set('tag_slug__and', $_GET['taglist']); }

return $query; }
add_action('pre_get_posts', 'advanced_search_query', 1000);

However, it displays ALL tags. How would I get specific tags by ID for visitors to select?

1 Answer 1



1) First, make sure you are using WordPress 4.3, which was just released, or this won't work.

2) Second, drop the entire pre_get_posts hook, you won't need it. WordPress will automatically filter the search archive based on the terms you've included in the querystring (this is the 4.3 functionality I just mentioned).

3) Next, replace your call to $tags = get_tags() with the following…

$tags = get_tags(
        'include' => array( 1, 17, 22 )

Simply change the include => array() array to use only the tag/term ids you want… alternatively, you might instead change include to exclude to show everything EXCEPT the tags you specify.

4) Finally, change the name of your checkbox inputs to tag[].

What We Did

The premise is this: WordPress 4.3 will automatically filter an archive page (including search results) based an array of querystring values you pass to it. This includes, for instance, "tag".

So, we can tell the get_tags() function to fetch only specific tags that we specify. It takes an associative array as an argument, and one of those is include, which takes another array of only the tag ids you want it to return.

To tie it all together, we need to make sure that the form is building the appropriate query string that WordPress expects. Something like yoursite.com/blog/?s=your+search+terms&tag%5B%5D=foo&tag%5B%5D=bar

To achieve that, we just make sure that the checkboxes have the name tag[] - which will be formatted by the querystring correctly, and then caught by WordPress which will use it to filter the search results.

  • Thanks for the fast response Matt! :-) When I do that, it doesn't show anything. Do I need to modify the functions.php part?
    – Jeremia
    Commented Aug 20, 2015 at 21:57
  • Actually, you can remove it entirely. If your querystring is properly formatted, WordPress will filter the search results by the provided terms automatically. That's probably why you're getting nothing. Commented Aug 20, 2015 at 22:24
  • I guess the part I'm messing up is when I replace it with that, then the labeling doesn't appear because I overwrite it with the code you gave? Sorry, total amateur here.
    – Jeremia
    Commented Aug 20, 2015 at 22:24
  • I expanded the answer with a much more detailed explanation. Commented Aug 20, 2015 at 22:56
  • Matt, you are AWESOME! If I can every repay the favor, please let me know! I did get a weird error on the search results page that I'm trying to figure out: "preg_match() expects parameter 2 to be string, array given" Is there any way to donate money to you/pay you for this?
    – Jeremia
    Commented Aug 20, 2015 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.