最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 any MODES using the OR parameter on the tax_query relation BUT not if I 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)

  1. 暂无评论