admin管理员组

文章数量:1304186

I want show (featured product) that instock, but this query just show in stock products.

$query = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 12,
        'orderby' => 'date',
        'order' => 'DESC',
        'meta_query' => array(
          'relation' => 'AND',
          array(
            'key' => '_stock_status',
            'value' => 'instock',
          ),
          array(
              'taxonomy' => 'product_visibility',
              'field' => 'name',
              'terms' => 'featured',
          ),
        ),
    );

I want show (featured product) that instock, but this query just show in stock products.

$query = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 12,
        'orderby' => 'date',
        'order' => 'DESC',
        'meta_query' => array(
          'relation' => 'AND',
          array(
            'key' => '_stock_status',
            'value' => 'instock',
          ),
          array(
              'taxonomy' => 'product_visibility',
              'field' => 'name',
              'terms' => 'featured',
          ),
        ),
    );
Share Improve this question edited Feb 3, 2021 at 9:40 och asked Feb 3, 2021 at 9:12 ochoch 391 silver badge8 bronze badges 2
  • Well, I guess you mistakenly put that meta query in the tax_query.. You should put it in a meta_query, i.e. 'meta_query' => array( array( ... ) ). – Sally CJ Commented Feb 3, 2021 at 9:26
  • 1 Thanks @SallyCJ , Now just show instock status – och Commented Feb 3, 2021 at 9:33
Add a comment  | 

1 Answer 1

Reset to default 1

You mixed meta query and tax query

It should look like this

$query = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => 12,
    'orderby' => 'date',
    'order' => 'DESC',
    'meta_query' => array(
      array(
        'key' => '_stock_status',
        'value' => 'instock'
      )
    ),
    'tax_query' => array(
      array(
          'taxonomy' => 'product_visibility',
          'field' => 'name',
          'terms' => 'featured'
      )
    )
);

本文标签: Meta Query relation “AND” not working