WP_query 返回所有帖子而不是自定义帖子类型

问题描述 投票:0回答:1

我正在一个 WP 网站上工作,我在这个 WordPress 查询中遇到了一个奇怪的问题,该查询应该仅返回自定义帖子类型

osku_infopost
的帖子。但相反,我得到了网站上的所有帖子(和页面,当然是
post_type
页面)。 奇怪的部分:这只发生在访问主页 URL (mysite.com) 时。如果我访问网站上的另一个页面(即 mysite.com/apage),查询将提供预期结果。

<?php 
// fetch all infoposts in a query
$args = array(
    'post_type'      => 'osku_infopost',
    'orderby'        => 'menu_order',
    'order'          => 'ASC',
    'posts_per_page' => -1,
);

$infopost_query = new WP_Query( $args ); ?>

<div class="infoposts-container">
    <ul id="infoposts">
    <?php while ( $infopost_query->have_posts() ) : $infopost_query->the_post(); ?>
        <li class="infopost-item">
            <?php echo get_the_title($infopost_query->post->ID); ?>
            <?php echo get_the_content($infopost_query->post->ID); ?>
        </li>   
    <?php endwhile; ?> 
    </ul>
</div>

似乎有什么东西正在注入查询,但我不明白在哪里或如何注入。

我可能在这里遗漏了一些非常明显的东西。有任何想法吗?

php wordpress custom-post-type
1个回答
0
投票

我发现了问题:我最初使用的functions.php中有一个过滤器,因为我使用主查询一次检索多个页面(

post_type
页面)。

function my_get_posts( $query ) {
  if ( is_home() && false == $query->query_vars['suppress_filters'] )
     $query->set( 'post_type', array( 'post', 'page') );
  return $query;
}
add_filter( 'pre_get_posts', 'my_get_posts' );

当此过滤器处于活动状态时,传递给

post_type
的数组中的
WP_query
字段(请参阅相关代码)似乎会被忽略,并且查询会自动返回
post_type
帖子和
post_type
页面的所有帖子,如中所述数组传递给
$query->set()
。感谢所有关注它的人!

© www.soinside.com 2019 - 2024. All rights reserved.