#Solution
Solution
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(
array(
'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
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.