gpt4 book ai didi

php - Woocommerce,使用 wp 查询按类别获取产品

转载 作者:行者123 更新时间:2023-12-05 00:40:28 25 4
gpt4 key购买 nike

我尝试了很多方法,但我无法显示具有类别名称或 id 的产品

                  $args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'cat' => 40,
'posts_per_page' => '12',
);

$loop = new WP_Query( $args );

最佳答案

试试这个例子,

因此,给定一个 ID 为 26 的类别,以下代码将返回它的产品 (WooCommerce 3+):

 $args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products = new WP_Query($args);
var_dump($products);

关于php - Woocommerce,使用 wp 查询按类别获取产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47649945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com