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

标签 php wordpress loops woocommerce product

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

代码:

$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/

相关文章:

PHP 升级问题 - 这是编码错误吗?

php - Javascript 正则表达式并获取所有匹配项,它类似于 preg_match_all(PHP)

javascript - 使用 javascript 或 jQuery 将 <a> 标签分配给 div

bash - 如何制作一个创建 40 个程序同时实例的 bash 脚本?

Javascript FOR 循环回调

javascript - 如何使用双for循环从json中检索过滤后的数据

php - 将 PHP 变量传递给 Javascript 动态添加的文本框

jQuery:如果用户已登录,请添加 atr 并将 css 应用于元素

php - Woocommerce 和 PayPal 账单地址

php - Magento 1.9 订单确认电子邮件购物车中的错误货币符号 - 使用 PayPal 付款时 - formatPrice()