Here's my query..

$params = array(
'posts_per_page' => -1,
'post_type' => 'product',
'orderby' => 'menu-order',
'order' => 'asc',
'fields' => 'ids',
//    'category_name' => 'stainless-steel-exhaust-ties',
'tax_query' =>
    'relation' => 'AND',
        'taxonomy' => 'product_visibility',
        'field' => 'term_id',
        'terms' => 7)
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => array( 'stainless-steel-exhaust-ties' )

I've tried using category_name in the original params.. as well as and AND relation in the tax_query

Using category_name does not display any results... using the second tax_query just gives me all the results.

How do I filter by category in my WP_Query?

  • 1
    are you sure it's category? and not cat or product_category? Also can you indent correctly and place brackets on the correct lines? My initial reading of your code suggested you were missing out closing brackets – Tom J Nowell Aug 14 '17 at 17:00
  • @brian use product_cat for woocommerce product category. – maheshwaghmare Aug 14 '17 at 19:13
  • @maheshwaghmare thank you! That at least worked without the AND relation.. will have to do some more testing to get that working. But so relieving to actually get the category and not every single post everytime :) – Brian Bruman Aug 14 '17 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.