Manipulate WP Query using a custom post type and multiple tax_query taxonomies

Question

I’m using the template_redirect action to hook into my archive page before and after the query using $_GET params for 2 different taxonomies. One for GAME and another for MODES. The query works perfectly on a single GAME and MODES using the OR parameter on the tax_query relation BUT not if if use AND, which is what I believe I need to be doing.

I want to use ANY single GAME and then ANY single MODE.

In my archive file I have an action before and after the loop:

<?php do_action('__before_loop'); ?>
<?php if ( have_posts() ) : //do stuff here...  endif; ?>
<?php do_action('__after_loop'); ?>

My Functions file:

add_action( 'template_redirect', 'hooks_setup' , 20 );
function hooks_setup() {
   add_action( '__before_loop'     , 'set_my_query' );
   add_action( '__after_loop'      , 'set_my_query', 100 );
}

My function to manipulate the query:

if( isset($_GET['mode']) && $_GET['mode'] !== 'all' && isset($_GET['game']) ){  

// hooks
    switch ( current_filter() ) {
            
        case '__before_loop':               
                                
                $taxquery = array(                      
                    'tax_query' => array(
                        'relation' => 'OR', // AND here doesn't show any results ???
                        array(
                            'taxonomy' => 'mode',
                            'field' => 'slug',
                            'terms' => $_GET['mode']
                        ),
                        array(
                            'taxonomy' => 'game',
                            'field' => 'slug',
                            'terms' => $_GET['game']
                        )
                    )               
                );

                            
            $wp_query = new WP_Query( $taxquery );
                        
        break;

        case '__after_loop': $wp_query = $wp_the_query;
        break;
0
Innate 3 months 0 Answers 4 views 0

Leave an answer