0

I am trying to query a custom post type based on what a user inputs into a form, I am having trouble querying multiple taxonomies in pre_get_posts filter, I can't seem to find a way to set the query more than one taxonomy

function custom_search( $query ) {
if ( is_archive('experts') && $query->is_main_query() && !is_admin() ) {

    $keyword = get_query_var( 'keyword', FALSE );
    $industry_select = strtolower(str_replace(' ', '-', get_query_var( 'industry_select', FALSE ) ));
    $speciality_select = strtolower(str_replace(' ', '-', get_query_var( 'speciality', FALSE ) ));
  
    // Keywords query
    $keyword ? $keyword : $keyword = null;

    $query->set('s', $keyword);
    
    // Taxonomies query

    $tax_query_array = array('relation' => 'OR');
    $speciality_select ? array_push($tax_query_array, array('taxonomy' => 'experts_specialities', 'field' => 'slug', 'terms' => $speciality_select) ) : null ;
    $query->set( 'tax_query', $tax_query_array);

    $tax_query_array = array('relation' => 'OR');
    $industry_select ? array_push($tax_query_array, array('taxonomy' => 'experts_industries', 'field' => 'slug', 'terms' => $industry_select) ) : null ;
    $query->set( 'tax_query', $tax_query_array);
}}
add_action( 'pre_get_posts', 'custom_search' );

In the past I have written queries that look like the following that successfully look in two taxonomies (eventually I need to have 3 taxonomies though)

$featured_args = array(
    'post_type'  => 'experts',
    'hide_empty' => false ,
    'paged'      => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'experts_industries',
            'field' => 'slug',
            'terms' => $broad_match_ind
        ),
        array(
            'taxonomy' => 'experts_specialities',
            'field' => 'slug',
            'terms' => $broad_match_spec
        )
    )
);

I'm trying to use pre_get_posts because I ultimately want the results of this query on an archive page and don't want to create a second query with a set of args ( as I am currently doing as evidenced by my second code example above )

Thanks in advance.

3
  • The 2nd $tax_query_array = array('relation' => 'OR'); overrides the previous array.. Also, is_archive() does not accept any parameters - use is_post_type_archive() to check for a specific post type archive.
    – Sally CJ
    Commented Apr 6, 2022 at 23:11
  • Ah yeah I see that now, thanks for the pointing that out. Probably staring at it too long.. So something the following should work? $tax_query_array = array('relation' => 'OR'); $industry_select ? array_push($tax_query_array, $tax_query_args ) : null ; $query->set( 'tax_query', $tax_query_array); }} ( I don't have a chance to try it out at the moment or I would just do that ), just asking for reference when I try tomorrow morning. Thanks. Commented Apr 7, 2022 at 3:43
  • See my answer :)
    – Sally CJ
    Commented Apr 7, 2022 at 10:11

1 Answer 1

2

I can't seem to find a way to set the query more than one taxonomy

You already found it, but the problem is, you should have not done this:

$tax_query_array = array('relation' => 'OR');
$speciality_select ? array_push($tax_query_array, ...) : null ;
$query->set( 'tax_query', $tax_query_array);

$tax_query_array = array('relation' => 'OR');
$industry_select ? array_push($tax_query_array, ...) : null ;
$query->set( 'tax_query', $tax_query_array);

.. because the 2nd $query->set() call overrides the entire tax_query array that's set via the 1st $query->set() call.

And to fix the issue, just remove the 1st $query->set() call and the 2nd $tax_query_array = array('relation' => 'OR');, i.e. lines 3 and 5 above.

Also, keep in mind that is_archive() does not accept any parameters. So if you wanted to check if the query is for a specific post type archive, use is_post_type_archive() instead, i.e. is_post_type_archive( 'experts' ).

And.. I don't think the $keyword ? $keyword : $keyword = null; is necessary, so I'd just remove that and simply do $query->set('s', $keyword);.

1
  • 1
    That'll do it! Thank you. I appreciate the help! Commented Apr 7, 2022 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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