按产品类型查询/过滤woocommerce产品

tri*_*tin 3 wordpress woocommerce

在这里添加了新的产品类型

现在我想展示那种产品类型.这是我的查询:

$query_args = array('post_type' => 'product' );

$r = new WP_Query( $query_args );

if ( $r->have_posts() ) { .........
Run Code Online (Sandbox Code Playgroud)

如何仅查询此新产品类型的产品?

hel*_*ing 11

在WooCommerce,"后键入"是一个自定义分类,所以你需要添加分类参数WP_Query.

$query_args = array(
   'post_type' => 'product',
   'tax_query' => array(
        array(
            'taxonomy' => 'product_type',
            'field'    => 'slug',
            'terms'    => 'your_type', 
        ),
    ),
 );
Run Code Online (Sandbox Code Playgroud)

其中terms参数与$this->product_type = 'your_type';新产品类型的类构造函数中的参数相同.