自定义wp_Query在索引页和类别页上不同

时间:2013-11-28 作者:Fyrebase

我一直在用手撞墙,想弄明白这一点。

有人能向我解释一下为什么这个自定义wp\\U查询会产生两个不同的sql语句,一个用于索引页(正确),另一个用于类别页(错误)

function myFunction( $category )
{
    if(!$category) return;

    $query = array(
        \'post_type\' => \'download\',
    );

    $query[\'tax_query\'][] = array(
        \'taxonomy\' => \'download_category\',
        \'terms\' => explode( \',\', $category ),
        \'field\' => \'slug\'
    );

    $downloads = new WP_Query( $query );

    return intval($downloads->found_posts);
}
索引sql(正确):

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (20) ) AND wp_posts.post_type = \'download\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'refunded\' OR wp_posts.post_status = \'failed\' OR wp_posts.post_status = \'revoked\' OR wp_posts.post_status = \'abandoned\' OR wp_posts.post_status = \'active\' OR wp_posts.post_status = \'inactive\' OR wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
类别sql(错误):

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (15) ) AND wp_posts.post_type IN (\'nav_menu_item\', \'post\', \'eddset\') AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'refunded\' OR wp_posts.post_status = \'failed\' OR wp_posts.post_status = \'revoked\' OR wp_posts.post_status = \'abandoned\' OR wp_posts.post_status = \'active\' OR wp_posts.post_status = \'inactive\' OR wp_posts.post_author = 1 AND wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
它正在忽略post\\u类型,但无法找出原因?

非常感谢您的帮助。

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

我只能假设类别页面上运行的过滤器正在覆盖您的帖子类型。

尝试添加\'suppress_filters\'=>true 但这只适用于某些过滤器,而不是所有过滤器。我怀疑这在这种情况下行不通post_type 参数,这是我假设过滤器所做的我希望选项2将是解决方案。

结束

相关推荐

按自定义域(wp-Query和循环)排序和显示帖子)

我只想在主页上显示即将到来的活动帖子。我使用“custom field suite”插件创建了一个自定义字段,名为“ENDS”,表示事件结束的日期。正如你所知,我只想在家里举行即将到来的活动,最接近现在的日期排在第一位。过期后,我希望将其归档到“归档”中,该页面在另一个页面上可见(该页面还应以最近过期的事件开始排序归档的事件帖子)。模板使用无限滚动加载帖子,这是我的索引文件,带有默认循环:http://pastebin.com/x3NzZBZX我需要集成这样的东西,但没有成功:<?php $