admin管理员组文章数量:1426058
My site is making a strange bug, the code below works 99% of the times but every now and then it return only 2 items in the loop, even that the query results have more products
// get the cats this product is in
$terms = wp_get_post_terms($post->ID, 'product_cat');
$cats = array();
$total = array();
// if there is only one category jump out.
if (count($terms) == 1) {
$cats = $terms[0]->term_id;
}else{
// remove anything that is a parent cat
foreach ($terms as $k => $term) {
if ($term->parent === 0) {
unset($terms[$k]);
} else {
// build list of terms we do want (children)
$cats[] = $term->term_id;
}
}
}
foreach ($terms as $k => $term) {
$total[] = $term->count;
}
// wp_reset_postdata();
$related_products = new WP_Query(
array(
'posts_per_page' => 3
'order' => 'DESC',
'orderby' => 'rand',
'post_type' => 'product',
'post__not_in' => $post->ID,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $cats,
)
),
)
);
I use this to check that the query results works fine every time, even when it only shows 2 results in the loop:
echo "<pre>";
var_dump($cats);
echo "</pre>";
echo 'Total products with these terms: '.array_sum($total);
echo "<pre>";
$total = $related_products->post_count;
echo 'Total in query: ';
var_dump($total);
// var_dump($related_products);
echo "</pre>";
you can check here under the Related Products section: /
It will mostly show fine, but sometimes if you refresh it will show only 2 products (like image attached)
Thanks a lot for any comments Santiago
本文标签: phpWpquery output correctbut the loop shows one less item (only sometimes)
版权声明:本文标题:php - Wp-query output correct, but the loop shows one less item (only sometimes) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745451123a2658889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论