WP Query causing timeout

时间:2021-06-26 作者:Thowzif

我试图显示一个油炸圈饼图的分类名称及其计数。这就是我所做的;

<ul class="pie-chart__legend" id="donut1">
<?php
$wcatTerms1 = get_terms(\'incident_type\', array(\'hide_empty\' => 0, \'parent\' =>0)); 
foreach($wcatTerms1 as $wcatTerm1) 
{

$args1 = array(
\'posts_per_page\' => -1,
\'post_type\' => \'incident\',
\'post_author\' => $current_user->ID,
\'post_status\' => \'publish\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'incident_type\',
\'field\' => \'term_id\', 
\'terms\' => $wcatTerm1, 
\'include_children\' => false
)
));
$count1 = 0;
$query1 = new WP_Query($args1);
while ( $query1->have_posts() ) 
{ 
$count1++;
}
?>
<li><em><?php echo $wcatTerm1->name; ?></em><span><?php echo $count1; ?></span></li>
<?php
}    
?>
但这是无限的,最终会超时。我尝试了几个涉及\\u posts()的修复方法,但没有成功。

请让我知道我做错了什么。

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

我已经弄明白了。我失踪了$query1->the_post(); 在while循环和wp_reset_postdata(); 循环结束后。

最终工作代码:

<ul class="pie-chart__legend" id="donut1">
                                    <?php
                                    $wcatTerms1 = get_terms(\'incident_type\', array(\'hide_empty\' => 0, \'parent\' =>0)); 
                                    foreach($wcatTerms1 as $wcatTerm1) 
                                    {
                                        
                                        $args1 = array(
                                        \'posts_per_page\' => -1,
                                        \'post_type\' => \'incident\',
                                        \'post_author\' => $current_user->ID,
                                        \'post_status\' => \'publish\',
                                        \'tax_query\' => array(
                                            array(
                                                \'taxonomy\' => \'incident_type\',
                                                \'field\' => \'term_id\', 
                                                \'terms\' => $wcatTerm1, 
                                                \'include_children\' => false
                                            )
                                        ));
                                        $count1 = 0;
                                        $query1 = new WP_Query($args1);
                                        while ( $query1->have_posts() ) 
                                        { 
                                            $query1->the_post();
                                            $count1++;
                                        }
                                        ?>
                                        <li><em><?php echo $wcatTerm1->name; ?></em><span><?php echo $count1; ?></span></li>
                                        <?php
                                        wp_reset_postdata();
                                    }    
                                    ?>
                                </ul>

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post