So I am trying to return a list of products that meet criteria specified by the user in a search. Everything in my original query has worked great so far, but I am now trying to add the functionality of only showing products in a specific category.

I’ve researched previous answers for the past hour and still am coming up short as to why my query isn’t returning any results.

$args = array(
                'post_type' => array('product', 'product_variation'),
                'tax_query' => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => $category
                        )
                ),
                'posts_per_page' => -1,
                'meta_query' => array(
                    array(
                    'key' => 'attribute_pa_width',
                    'value' => $width,
                    'compare' => 'LIKE'
                    ),
                    array(
                    'key' => 'attribute_pa_profile',
                    'value' => $profile,
                    'compare' => 'LIKE'
                    ),
                    array(
                    'key' => 'attribute_pa_rim',
                    'value' => $rim,
                    'compare' => 'LIKE'
                    )
                )
        );


            }

                $loop = new WP_Query( $args );

                if ( $loop->have_posts() ) {
                    while ( $loop->have_posts() ) : $loop->the_post();
                        wc_get_template_part( 'content', 'product' );
                    endwhile;
                } else {
                    echo __( 'No products found' );
                }
                wp_reset_postdata();
            ?>

Now the $category variable is exactly identical to the slug of the category I am trying to query so that isn’t the issue. Also, when I take out the tax_query, a list of products shows – which means the meta_query and the rest of it is fine as well.

And yes – there are products with the category already selected.

Any help is appreciated – thanks,

Michael

Read more here: WordPress tax_query in WP_Query not working


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: