'Problem filtering wordpress custom post type using get_query_var()
I'm new in the forum so I apologize for any mistake I could make. I'm customizing a custom post type archive page and while creating it, I'd like to insert a list of tags on the sidebar in order to let the user filter custom posts.
Specifically, I created a 'hotels' custom post type and in each post there are a few tags used. I did not create a custom taxonomy because some tags are used in other posts type too.
So, in order to show the user only the 'hotels' post type used tags I am calling wordpress' get_tags()
function passing a list of the wanted tags' IDs.
After getting all the tags I started a foreach
loop that creates an unorder list of input elements. Every input element contains some attributes in order to check if it is contained in the url.
Now the problem is that if i check multiple tags on my sidebar and click on the apply filters button, the new url with query vars will show me every post that contains at least one of the checked tags. How can I modify the whole thing in order to show only posts that must contain every tag that is present in the url query?
Hope I've been clear, I'll post the code above. Thanks in advance for any help.
<?php $hotel_tags = get_tags('include=2,5,11,12,13,22,25,26,27,28,30,31,34,35,41');?>
<form method="GET">
<ul class="tag__line">
<?php foreach($hotel_tags as $tag):?>
<li>
<label>
<input type="checkbox" name="terms[]" value="<?php echo $tag->slug; ?>" <?php checked((isset($_GET['terms']) && in_array($tag->slug, $_GET['terms']))) ?>/>
<?php echo $tag->name; ?>
</label>
</li>
<?php endforeach;?>
</ul>
<button type="submit">Applica filtri</button>
</form>
Solution 1:[1]
I've found a solution, it actually was pretty easy.
I just had to declare in my functions.php file the query vars in order to get them working with get_query_var()
.
After that I can easily grab them and insert into WP_Query $args
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Richard |