gpt4 book ai didi

php - WooCommerce 通过属性查询获取产品

转载 作者:行者123 更新时间:2023-11-28 01:14:57 25 4
gpt4 key购买 nike

我有一个带有属性颜色的产品。属性值为红色、蓝色和绿色。我正在尝试创建自定义搜索,但我无法获得提取任何产品的查询。

$args =  array(
'post_type' => array('product'),
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN',
)
),
'tax_query' => array(
array(
'taxonomy' => 'product',
'field' => 'slug',
'terms' => array('blue', 'red', 'green'),
'operator' => 'IN',
),
)
);

$products = new WP_Query( $args );

我哪里出错了?

最佳答案

产品属性颜色的正确分类是 'pa_color',因此正确的工作查询是:

// The query
$products = new WP_Query( array(
'post_type' => array('product'),
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN',
) ),
'tax_query' => array( array(
'taxonomy' => 'pa_color',
'field' => 'slug',
'terms' => array('blue', 'red', 'green'),
'operator' => 'IN',
) )
) );

// The Loop
if ( $products->have_posts() ): while ( $products->have_posts() ):
$products->the_post();
$product_ids[] = $products->post->ID;
endwhile;
wp_reset_postdata();
endif;

// TEST: Output the Products IDs
print_r($product_ids);

此代码已经过测试并且可以工作。您将获得具有值(术语)“蓝色”、“红色”和“绿色”的 Color 属性的所有产品......

Since WooCommerce 3, product visibility is handled by custom taxonomy product_visibility. You can see the following related threads:

关于php - WooCommerce 通过属性查询获取产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51909600/

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