use*_*629 6 php wordpress attributes product woocommerce
我有一个属性颜色的产品.属性值为红色,蓝色和绿色.我正在尝试创建自定义搜索,但我无法获取查询以提取任何产品.
$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 );
Run Code Online (Sandbox Code Playgroud)
我哪里做错了?
产品属性颜色的正确分类是'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);
Run Code Online (Sandbox Code Playgroud)
此代码经过测试和运行.您将获得具有Color属性的所有产品,其值(条款)为'blue','red'和'green'...