If I have a list of term ids, like
1,2,3,4,5 that correspond to tags, categories and custom taxonomies, how can I get all the posts that contain all these terms?
Let's say from that list of term ids, they belong to these taxonomies:
2 are tags,
3 is a category,
4 is custax1, and
5 is custax2.
I want to pass
get_posts() or something else so that it returns only posts that contain all of these term ids, regardless what taxonomy those ids belong to. How do I do this?
Details, in case they help:
I am building a site that utilizes the categories, tags, and two custom taxonomies, a total of four taxonomies. I am building a filter page that allows the user to sift though all four of these taxonomies and select which posts to show. The user clicks on labels for different taxonomy names then the term_ids are passed into a URL variable and the page is reloaded (e.g. the url contains
?terms=1,2,3,4,5). After the page reloads, the filter shows only posts that contain all the selected term_ids (whether tags, categories, or custom taxonomies).
I'm struggling on the part where the filter actually displays the results. There doesn't seem to be any way to fetch posts filtered on term_id without knowing what taxonomy a term_id corresponds to.
In looking through all the WP Query Class arguments I see that I can target term_ids, but cannot seemingly target all taxonomies at the same time. It looks like I have to target tags, categories, and custom taxonomies with
'tax_query' => array(), but there's still an issue there. You cannot pass term ids that are not part of the stated taxonomy or you'll get an empty array returned.
Here's some example arguments for
$args = array( 'numberposts' => -1, 'post_type' => array('post'), 'post_status' => 'publish', 'tag__and' => array(), // gets posts with these tag term ids, // but cannot pass non-tag term ids. 'category__and' => array(), // gets posts with these cat term ids, // but cannot pass non-category term ids. 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'custax1', 'field' => 'term_id', 'terms' => array(), // gets posts with these custom tax term ids, // but cannot pass non-custax1 term ids ), array( 'taxonomy' => 'custax2', 'field' => 'term_id', 'terms' => array(), // gets posts with these custom tax term ids, // but cannot pass non-custax2 term ids term ids ),
I can't just pass the URL variable as an array to any of these unless it contains only term ids for within that taxonomy. If I pass an array of term ids that consist of tags and categories, I will get zero results on all the arrays in the above code. I'm hoping I'm missing something simple here, and can easily pass the URL variable to a function that then gets the posts that contains all the term_ids.