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'); ?>">
<fieldset>
<input type="text" name="s" value="" placeholder="search…" maxlength="50" required="required" />
<p>Refine search to posts containing chosen tags:</p>
<?php
// generate list of tags
$tags = get_tags();
foreach ($tags as $tag) {
echo
'<label>',
'<input type="checkbox" name="taglist[]" value="', $tag->slug, '" /> ',
$tag->name,
"</label>\n";
}
?>
<button type="submit">Search</button>
</fieldset>
</form>
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?