gpt4 book ai didi

php - 按自定义产品属性过滤产品并在 WP_Query 中发布元数据

转载 作者:搜寻专家 更新时间:2023-10-31 20:57:35 24 4
gpt4 key购买 nike

我已经按类别过滤了,但不知道如何按自定义属性或元值过滤产品

代码:

$key="naam"; //custom attribute name
$value="test";// custom value
$query_custom = array('key' => $key, 'value' => $value);
$meta_query[] = $query_custom ;

$args=array('meta_query'=>$meta_query, 'product_cat' => 'activiteiten','posts_per_page' => 10,'post_type' => 'product');



$loop = new WP_Query( $args );

`

See image for custom attributes

最佳答案

这是一个查询的工作示例,涉及:

  • 产品类别过滤
  • 发布元值过滤(元查询)
  • 商品属性值过滤(税务查询)

代码:

$products = new WP_Query( array(
'posts_per_page' => 10,
'post_type' => 'product',
'post_status' => 'publish',

// 1. Product category filter
'product_cat' => 'clothing',

// 2. The Post meta query part (filtering by post meta value)
'meta_query' => array( array(
'key' => '_price',
'value' => 5,
'type' => 'numeric',
'compare' => '>',
), ),

// 3. The taxonomy meta query part (filtering by term values)
'tax_query' => array( array(
'taxonomy' => 'pa_color', // Product attribute taxonomy: always start with 'pa_'
'field' => 'slug', // Can be 'term_id', 'slug' or 'name'
'terms' => array('blue'),
), ),
) );

// Testing output
if( $products->have_posts() ) :
echo '<ul>'
while ( $products->have_posts() ) : $products->the_post();
echo '<li class="post-id-' . get_the_id() . '">' . get_the_title() . '</li>';
endwhile;
wp_reset_postdata();
echo '</ul>'
endif;

测试和工作:

Wordpress WP_Query 的官方引用文档:

关于php - 按自定义产品属性过滤产品并在 WP_Query 中发布元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54004962/

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