• Hi!

    I experienced the same issue on tax query.

    The issue is that if you provide many keywords for one taxonomy array in tax query, you will only receive the first item for the post. check my code

    $args = array(
    	'post_type' => 'product',
    	'posts_per_page' => -1,
    	//'paged' => $paged,
    	'tax_query'      => array( 		
            'relation' => 'IN',
    		array(
    			'taxonomy' =>   'pa_size-mm',
    			'field'    =>   'slug',
    			'terms'    =>    array('10','8-24-x-7-82'),
                'operator'        => 'OR',
    		),		
    	)
    );
    
    $loop = new WP_Query( $args );
    $product_count = $loop->post_count;
    
    while ( $loop->have_posts() ) : $loop->the_post(); 
    global $product;
    global $post;
    $test = $loop->post->ID;
    echo $test;
    echo "<br>";
    
    endwhile;

    Kindly help me
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @thilipdonmax29! According to the WP_Query documentation around taxonomy queries, it looks like the relation field says the following:

    The logical relationship between each inner taxonomy array when there is more than one. Possible values are ‘AND’, ‘OR’. Do not use with a single inner taxonomy array.

    In the query you supplied, your value for relation is IN (which does not appear to be one of the allowed values) and additionally you are using it with a single inner taxonomy array. I would recommend removing the relation mapping altogether for this search.

    Additionally, in the inner taxonomy array you’re also using 'operator' => 'OR'. The documentation for operator states:

    Operator to test. Possible values are ‘IN’, ‘NOT IN’, ‘AND’, ‘EXISTS’ and ‘NOT EXISTS’. Default value is ‘IN’.

    It does not appear that OR is an allowed option there either, so I’d recommend removing that parameter as well. Hope that helps!

    Thread Starter thilipdonmax29

    (@thilipdonmax29)

    Hi Madison,

    I refactor the wp_query but I am getting only first taxonomy only

    $args = array(
    	'post_type' => 'product',
    	'posts_per_page' => -1,
    	//'paged' => $paged,
    	'tax_query'      => array( 	
    		array(
    			'taxonomy' =>   'pa_size-mm',
    			'field'    =>   'slug',
    			'terms'    =>    array('10','8-24-x-7-82'),
    		),		
    	)
    );
    
    $loop = new WP_Query( $args );
    $product_count = $loop->post_count;
    
    while ( $loop->have_posts() ) : $loop->the_post(); 
    global $product;
    global $post;
    $test = $loop->post->ID;
    echo $test;
    echo "<br>";
    
    endwhile;

    Thanks

    Moderator bcworkz

    (@bcworkz)

    I tested your code on my site, altering assigned values to fit my existing data. I get all products assigned either taxonomy term. There may be other code altering your query that’s confusing the results. Try echoing out $loop->request (the actual SQL query) and see if anything is in there that shouldn’t be.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query only returns posts that match first term in tax_query array’ is closed to new replies.