• Hi,

    Im trying to show posts that are in a category and with a tag. I believe this is now possible since 3.1 using tax_query. I have the following code to show all posts in the category of ‘blog’ and with a tag of ‘evening’ but it doesn’t return any results. Any help in fixing it would be much appreciated.

    $args = array(
    	'tax_query' => array(
    		'relation' => 'AND',
    		array(
    			'taxonomy' => 'category',
    			'terms' => array('blog'),
    			'field' => 'slug',
    		),
    		array(
    		'taxonomy' => 'post_tag',
    		'terms' => array('evening'),
    		'field' => 'slug',
    		),
    	),
    );
    $query = new WP_Query( $args );

    [Please post code snippets between backticks or use the code button.]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter paa1605

    (@paa1605)

    Ok so i’ve managed to get this working but for some reason the ‘orderby’ and ‘order’ arguments are being ignored. The posts are shown in the same order no matter what. Any suggestions as to how i can fix this?

    $args = array(
    		  'orderby' => 'ID',
    		  'order' => 'DESC',
    		  'posts_per_page' => '3',
    		  'tax_query' => array(
    		  'relation' => 'AND',
    		  array(
    		  'taxonomy' => 'category',
    		  'terms' => array('24'),
    		  'field' => 'term_id',),
    		  array(
    		  'taxonomy' => 'post_tag',
    		  'terms' => array('46','47','49','50','51'),
    		  'field' => 'term_id',
    		  ),
    		  ),
    		  );

    I know this post is old but the orderby and order go as shown below:

    $args = array(
    	'tax_query' => array(
    		'relation' => 'AND',
    		array(
    			'taxonomy' => 'category',
    			'terms' => array('blog'),
    			'field' => 'slug',
    		),
    		array(
    		'taxonomy' => 'post_tag',
    		'terms' => array('evening'),
    		'field' => 'slug',
    		),
    	),
            'orderby' => 'ID',
    	'order' => 'DESC',
    );
    $query = new WP_Query( $args );

    Or maybe not as your code is slightly different to mine, might go after the $args in WP_Query?

    Mine:

    $taxonomy_terms = get_terms( $taxonomy, array(
    			    'hide_empty' => 0,
    			    'fields' => 'ids'
    			) );
    
    			// Use the new tax_query WP_Query argument (as of 3.1)
    			$food = new WP_Query( array(
    			    'tax_query' => array(
    			        array(
    			            'taxonomy' => $taxonomy,
    			            'field' => 'id',
    			            'terms' => $taxonomy_terms
    			        ),
    			    ),
    			            'orderby' => 'menu_order',
    			            'order' => 'asc'
    			) );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Tax_query code not working. Help needed’ is closed to new replies.