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]),
            ) ) );
        }
    }   

2 Answers
2

Think of this pseudo code

if sky == blue
  set a = 5
if grass == green
  set a = 7

What value will a have? Not 12.

This is the exact same situation. You are setting a specific parameter to a specific value. In the second call, you are overwriting your previous value. To avoid this, you can build the value (here the array) beforehand, and call ->set() only once.

$tax_query = array();
if( isset( $rt_term_id ) && ! empty( $rt_term_id ) ) {
    $tax_query[] = array(
        'taxonomy'  => 'vakantiesoorten_listing',
        'field'     => 'id',
        'terms'     => array($rt_term_id[0]),
    );
}
if( empty($_GET['location_geo_data']) && isset( $rt_term_id_land ) && ! empty( $rt_term_id_land ) ) {
    $tax_query[] = array(
        'taxonomy'  => 'landen_listing',
        'field'     => 'id',
        'terms'     => array($rt_term_id_land[0]),
    );
}
$query->set( 'tax_query', $tax_query );

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *