I am using pre_get_posts hook in order to filter posts by custom terms. All working fine, but if i want to filter posts by 2 custom terms, the query only filters by the last term given. See my code below. I hook this into pre_get_posts but when both if statements are TRUE, only the last $query->set is done, meaning it won’t filter the posts twice. Is there any way to accomplish this? Thanks
//For searching
if( $query->is_main_query() && isset( $_GET[ 'ls' ] ) ) {
$rt_term_id = $_GET['listing_soort'];
$rt_term_id_land = $_GET['listing_land'];
// IF our soort vakantie is set and not empty - include it in the query
if( isset( $rt_term_id ) && ! empty( $rt_term_id ) ) {
$query->set( 'tax_query', array( array(
'taxonomy' => 'vakantiesoorten_listing',
'field' => 'id',
'terms' => array($rt_term_id[0]),
) ) );
}
// IF our land vakantie is set and not empty - include it in the query
if( empty($_GET['location_geo_data']) && isset( $rt_term_id_land ) && ! empty( $rt_term_id_land ) ) {
$query->set( 'tax_query', array( array(
'taxonomy' => 'landen_listing',
'field' => 'id',
'terms' => array($rt_term_id_land[0]),
) ) );
}
}