I am trying to use a tax query in my pre_get_posts
function. All is working but not when the array has multiple IDs. Let's say my URL looks like: ?listing_cat[]=1&listing_cat[]=2
Now I am trying to filter out posts that are in those 2 categories. But my filter only gets the posts from the first category ID. I guess I will need a foreach function, but I am not sure how to implement this. Currently I have the code below. Now I know I can use filter by multiple terms with a comma, like : 'terms' => array($rt_cat_id[0], $rt_cat_id[1])
, but I just need it to automatically get all the terms from the array $rt_cat_id
and use those to filter the posts. How to accomplish that? Thanks
add_action( 'pre_get_posts', 'rt_tax_archive' );
function rt_tax_archive($query) {
$rt_cat_id = $_GET['listing_cat'];
if( isset( $rt_cat_id ) && ! empty( $rt_cat_id[0] ) ) {
$tax_query[] = array(
'taxonomy' => 'listing_category',
'field' => 'id',
'terms' => array($rt_cat_id[0]),
);
}
}