3

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

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 );
  • 1
    Thank you so much.. I understood the why, just not the how.. This makes sense. Thanks! – RobbTe Jul 20 '17 at 14:27
  • One more question, to be sure; $query->set( 'tax_query') respects search variables, other than taxonomies, that are already in the search string right? So if someone is doing a text search and filters it with a custom term, the text search is respected, correct? Thanks – RobbTe Jul 20 '17 at 20:16
  • 1
    @RobbTe Yes. You can look at a sample WP Query object. Since you are only (over)writing the tax_query, this will have no influence on the other search variables – kero Jul 21 '17 at 10:14
1

You can do this, you just need to get the original value of the tax query, add the additional query, then set it again. The abbreviated version is:

// Get current tax query if it exists, otherwise get an empty array.
$tax_query = $query->get( 'tax_query' ) ?: array();

if ( true ) {
    $tax_query[] = array(); // Add first tax query.
}

if ( true ) {
    $tax_query[] = array(); // Add second tax query.
}

$query->set( 'tax_query', $tax_query ); // Set tax query parameter on query.
  • Hi, thanks. I guess i understand your logic, but how do i set the 2 filters at //ad first tax query and //add second tax query. I mean i have to filter them for the term_id given right? Please further elaborate. – RobbTe Jul 20 '17 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.