WP_Query 只退回一个 WooCommerce 产品
WP_Query only returning one WooCommerce product
我正在尝试遍历特定类别中的所有 WooCommerce 产品。
它应该像这样简单:
$args = array(
'post_type' => 'product',
'tax_query' => array(array(
'taxonomy' => 'product_cat',
'terms' => (int)$cat->term_id,
'posts_per_page' => -1
))
);
$product_query = new WP_Query($args);
if ($product_query->have_posts()) {
echo '<p>';
while($product_query->have_posts()) {
$product_query->the_post();
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
}
echo '</p>';
wp_reset_postdata();
}
然而,当我在该类别中有 3 个产品时,它只有 returns 1 个产品。如果我回显 $product_query->found_posts
它说 3。我做错了什么?
我花了 30 分钟调试 WordPress 核心才意识到 posts_per_page
参数进入 $args['posts_per_page']
而不是 $args['tax_query'][0]['posts_per_page']
我正在尝试遍历特定类别中的所有 WooCommerce 产品。
它应该像这样简单:
$args = array(
'post_type' => 'product',
'tax_query' => array(array(
'taxonomy' => 'product_cat',
'terms' => (int)$cat->term_id,
'posts_per_page' => -1
))
);
$product_query = new WP_Query($args);
if ($product_query->have_posts()) {
echo '<p>';
while($product_query->have_posts()) {
$product_query->the_post();
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
}
echo '</p>';
wp_reset_postdata();
}
然而,当我在该类别中有 3 个产品时,它只有 returns 1 个产品。如果我回显 $product_query->found_posts
它说 3。我做错了什么?
我花了 30 分钟调试 WordPress 核心才意识到 posts_per_page
参数进入 $args['posts_per_page']
而不是 $args['tax_query'][0]['posts_per_page']