• 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 in functions.php:

    // 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);

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

Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Allow visitors to search by multiple tags (specific IDs)’ is closed to new replies.