wordpress - 当有匹配的帖子时,WP_Query 有时会返回零结果 - 为什么?

标签 wordpress

这个让我难住了。

我有一个包含这个循环的 category.php 文件:

<?php
if ( have_posts() ) : ?>
    <?php
    while ( have_posts() ) : the_post(); ?>
        <div class="entry-content description clearfix">
            <h2 class="category-subtitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php echo the_content(); ?>
            <?php global $withcomments; $withcomments = 1;
    ?>
        </div><!-- .entry-content -->
    <?php
    endwhile;
else :
    get_template_part( 'content', 'none' );
endif;
?>

这段代码工作正常,并且总是返回预期的结果。

此外,在循环之外(在它之后,以防万一),我在此循环的一侧有一列 - 为清楚起见,我将其称为新闻源循环:
<h3 class="newsfeed-heading"><a href="/category/news/">Latest News</a></h3>
<?php
    // wp_reset_query(); * Same results with or without wp_reset_query
    $args = array(
    'cat' => 89,
    'order'   => 'ASC'
    );
    $custom_query = new WP_Query($args);
    //echo "<h2>Found: $custom_query->found_posts</h2>";
    while($custom_query->have_posts()) : $custom_query->the_post();
?>
    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <h4 class="highlight1"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <p><?php $content = get_the_content(); echo mb_strimwidth($content, 0, 160, '...');?></p><div class="morelink"><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>">Read more</a></div>
    </div>
<?php endwhile; 
  // wp_reset_postdata();  * Same results with or without wp_reset_postdata
?>

现在,在大多数情况下,这两个循环可以很好地结合在一起,并且两个循环都返回我期望的结果集。但不总是。

据我所知,我认为如果主类别循环只包含一个结果,那么新闻源循环总是正确的。但是如果主类别循环在结果集中有多个帖子,则新闻源循环通常可以正常工作。所以我一直无法发现一个模式。

我试过把 wp_reset_query();在不同的地方,抓着稻草,但没有任何区别。

顺便说一下,当新闻源循环工作时,它总是返回正确的结果集。但是当它不返回时,它总是不返回任何内容和 $custom_query->found_posts返回零。

我真的很感激一些关于在哪里寻找可能的解决方案的建议。

最佳答案

对于任何搜索,found_posts 返回 0 如果参数正在使用 'no_found_rows' => true .

关于wordpress - 当有匹配的帖子时,WP_Query 有时会返回零结果 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31547473/

相关文章:

php - 将杂散文本包裹在 <p> 标签中

php - 如何使用 jQuery 将不同的 FontAwesome 图标填充到 WordPress 类别列表中?

php - 需要从站点禁用自动下载器

html - Nivo Slider 自定义高度/宽度问题

php - 保存帖子类型时运行 在 WordPress 中运行函数

javascript - Wordpress dFactory Cookie 通知插件

php - 如何在functions.php中正确创建自定义表单的链接

jquery - 试图从鼠标悬停更改为单击

javascript - 显示包含特定字符串的 div

html - 缩小比例以响应时,如何将这些图像定位在 Logo 下方?