为什么META_QUERY会破坏此WP_QUERY?

时间:2016-12-21 作者:markratledge

我很困惑:为什么要添加meta_query 数组是否中断此WP\\U查询?调试时未显示错误。我不知道还能尝试什么。

这很好:

$args = array( 
                \'category__in\' => array( 24,420,421 ),
                \'post_type\' => \'post\',  
                \'post_status\' => \'publish\',  
                \'posts_per_page\' => 15,
                \'posts_per_archive_page\' => 15, 
                \'nopaging\' => false, 
                \'paged\' => get_query_var(\'page\'),  
                \'orderby\' => \'date\'
                );

                $args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
                $the_query = new WP_Query( $args );
                $temp_query = $wp_query;
                $wp_query   = NULL;
                $wp_query   = $the_query;

                if ( $the_query->have_posts() ) :
                while ( $the_query->have_posts() ) : $the_query->the_post(); 
                $even_odd_class = $the_query->current_post % 2 == 0 ? \'even\' : \'odd\';
但只要我加上meta_query\' => array, 它中途中断,不显示15个帖子。

 $args = array( 
            \'category__in\' => array( 24,420,421 ), 
            \'meta_query\' => array(
                array(
                    \'key\' => \'featured-checkbox\',
                    \'value\' => \'no\', \'meta_compare\' => \'=\' )
                ),
            \'post_type\' => \'post\',  
            \'post_status\' => \'publish\',  
            \'posts_per_page\' => 15,
            \'posts_per_archive_page\' => 15, 
            \'nopaging\' => false, 
            \'paged\' => get_query_var(\'page\'),  
            \'orderby\' => \'date\'
            );

            $args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
            $the_query = new WP_Query( $args );
            $temp_query = $wp_query;
            $wp_query   = NULL;
            $wp_query   = $the_query;

            if ( $the_query->have_posts() ) :
            while ( $the_query->have_posts() ) : $the_query->the_post(); 
            $even_odd_class = $the_query->current_post % 2 == 0 ? \'even\' : \'odd\';
所用循环的(缩写)其余部分:

the_content;

endwhile;

the_posts_pagination();

else : get_template_part( \'template-parts/content\', \'none\' ); endif;
•以及featured-checkbox 在WP Admin中的自定义字段查看器中yesno.

Update: 良好点re:var\\u转储;我得到了

字符串(562)“选择SQL\\u CALC\\u FOUND\\u ROWS wpiv\\u posts.ID FROM wpiv\\u posts LEFT JOIN wpiv\\u term\\u relationships ON(wpiv\\u posts.ID=wpiv\\u term\\u relationships.object\\u ID)内部连接wpiv\\u Posteta ON(wpiv\\u posts.ID=wpiv\\u Posteta.post\\u ID),其中1=1和(wpiv\\u term\\u relationships.term\\u taxonomy\\u ID IN(24420421))和((wpiv\\u Posteta.meta\\u key=\'特色复选框\'和wpiv\\u Postm\\u eta.meta\\u值=\'否\'))和wpiv\\U岗位。post\\u type=“post”和((wpiv\\u posts.post\\u status=“publish”))按wpiv\\u posts分组。wpiv\\U发布的ID订单。menu\\u order,wpiv\\u posts。post\\u日期描述限制0,15“

当我在数据库中运行它时,我得到了7篇文章,与显示的内容相同。但我在这三个类别中有250多个帖子,只有2个有meta_query 设置为yes.

1 个回复
SO网友:wassimo

try to add wp_reset_query() in the end of your query