• Hello,

    Below is the code for my advanced search page which has checkboxs for categories and custom tags. What I want it to do with the Facilities taxonomy is when multiple are selected it will only return posts that are marked with all the selected tags.

    At the moment it it will pass back all posts that contain one or more of the selected facilities.

    so it seems to be searching ‘or’ instead of ‘and’ if you get what I mean.

    I can’t seem to figure it out, any help?

    <form role="search" method="get" id="Advsearchform" action="{$homeUrl}">
                <div>
                    <label id="AdvSeaBox"><!-- for="s">Search for:-->
                    <input type="text" value="" name="s" id="s" />Search for:</label>
                    <!--put category id in the value*/-->
                    <div id="AdvSearchCat">
                    <h2>In</h2>
                        <ul>
                            <li><label><input type="checkbox" name="categories" id="dir-searchinput-category-id" value="5"><span class="SeaCatIcon" id="AdSeoAtt"></span><p>Attractions</p></label></li>
                            <li><label><input type="checkbox" name="categories" id="dir-searchinput-category-id" value="6"><span class="SeaCatIcon" id="AdSeoStay"></span><p>Places to stay</p></label></li>
                            <li><label><input type="checkbox" name="categories" id="dir-searchinput-category-id" value="7"><span class="SeaCatIcon" id="AdSeoEat"></span><p>Places to eat</p></label></li>
                        </ul>
                    </div>
                    <div id="AdvSearchLoco">
                    <h2>Location</h2>
                    <ul>
                    <?php
                            // The following will list all locations with a checkbox next to them.
                            $locos = get_terms( 'ait-dir-item-location' );
                            $checkboxes = '';
                            foreach($locos as $loco) :
                                $checkboxes .='<li><input type="checkbox" name="locations" value="'.$loco->term_id.'" id="tag-'.$loco->term_id.'" /><label for="tag-'.$loco->term_id.'">'.$loco->name.'</label></li>';
                            endforeach;
                            print $checkboxes;
                            ?>
                    <li><label><input type="checkbox" name="All" id="dir-searchinput-location-id" value="8,10,11,12">All Locations</label></li>
                    </ul>
                    </div>
                    <div id="AdvSearchFaci">
                    <h2>Facilities</h2>
                    <ul>
                    <?php
                            // The following will list all facilities tags with a checkbox next to them.
                            $tags = get_terms( 'facilities' );
                            $checkboxes = '';
                            foreach($tags as $tag) :
                                $checkboxes .='<li><label for="tag-'.$tag->term_id.'"><input type="checkbox" name="facilities" value="'.$tag -> slug.'" id="tag-'.$tag->term_id.'" /><span class="FacIcons" id="Icon-'.$tag-> slug.'"></span>'.$tag->name.'</label></li>';
                            endforeach;
                            print $checkboxes;
                            ?>
                    </ul>
                    </div>
                    <input type="submit" id="Advsearchsubmit" value="Search" />
                </div>
    		</form>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter liamo1988

    (@liamo1988)

    After checking further it seems to be only displaying results for the last selected check box I.E. if I choose two facilities, the search url would be: /?s=&facilities=baby-changing&facilities=conference-facilities , and only results including conference facilities would be shown.

    Thread Starter liamo1988

    (@liamo1988)

    The search passes the following when multiple checkboxes are ticked: /?s=&facilities=baby-changing&facilities=conference-facilities When it needs to be: /?s=&facilities=baby-changing+conference-facilities for it to return results that contain both the facilities selected. Any Ideas?

    Thread Starter liamo1988

    (@liamo1988)

    I have also tried the following with no success

    the following in my searchform:

    // generate list of tags
    $tags = get_terms('facilities');
    foreach ($tags as $tag) {
        echo
            '<label>',
            '<input type="checkbox" name="taglist[]" value="',  $tag->slug, '" /> ',
            $tag->name,
            "</label>\n";
    }

    And the following in functions.php

    add_action('pre_get_posts', 'advanced_search_query', 1000);
    
    // 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;
        }
    
    }

    Any help would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Search, custom taxonomy 'all selected taxonomy terms'’ is closed to new replies.