• Ok so here goes, i have created a custom search, i want my users to be able to search for businesses (custom post type) and filter them by two taxonomies(location and category). I have this working, so far by using the following searchform

    <form role="search" method="get" id="searchform"
        class="searchform" action="<?php echo esc_url( home_url( '/a3/' ) ); ?>">
        <div>
            <fieldset>
            <label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label>
            <input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
            <?php #echo buildSelect('business-category') ?>
            <select class="select2" name="business-category">
              <?php
                // generate list of categories
                $taxonomies = get_terms('business-category');
                foreach ($taxonomies as $tax) {
                	echo '<option value="', $tax->slug, '">', $tax->name, "</option>\n";
                }
              ?>
            </select>
            <select name="location">
              <?php
                // generate list of categories
                $taxonomies = get_terms('loc');
                foreach ($taxonomies as $tax) {
                  echo '<option value="', $tax->slug, '">', $tax->name, "</option>\n";
                }
              ?>
            </select>
    
            <input type="submit" id="searchsubmit"
                value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" />
            </fieldset>
        </div>
    </form>

    and adding this to my functions

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

    what i am trying to achieve is to have the option when the user selects a category or location or both but does not enter a search term to list said category, location or join of the two.

  • The topic ‘Custom search results by taxomony’ is closed to new replies.