WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] limit multiple categories tax_query by tag not working (3 posts)

  1. shagen
    Member
    Posted 3 years ago #

    Displaying posts that are tagged with 'resource'. The following code is working great...

    $querystring = array(
    		'cat' => 'category__in => array('.$cat.','.$topic.')',
    		'posts_per_page' => -1,
    		'tag' => 'resource',
    		'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    	);

    However this code is working but not honoring the 'resource' tag...

    $pagesorter['tax_query'] = array(
    		'tag' => 'resource',
    		'posts_per_page' => -1,
    		array(
    			'taxonomy' => 'category',
    			'terms' => $cat,
    			'field' => 'id',
    		),
    		array(
    			'taxonomy' => 'category',
    			'terms' => $topic,
    			'field' => 'id',
    		),
    	);

    How do I get the above piece of code to honor the posts that are tagged 'resource'? I am sure I have something out of order or wrong. I have tried putting it within each array with no luck as follows.

    $pagesorter['tax_query'] = array(
    		'posts_per_page' => -1,
    		array(
    			'taxonomy' => 'category',
    			'terms' => $cat,
    			'field' => 'id',
    			'tag' => 'resource',
    		),
    		array(
    			'taxonomy' => 'category',
    			'terms' => $topic,
    			'field' => 'id',
    			'tag' => 'resource',
    		),
    	);

    Any thoughts, ideas and help would be greatly appreciated! Thanks for your time everyone!

  2. scribu
    Member
    Posted 3 years ago #

    You can't put 'tag' and 'posts_per_page' directly in 'tax_query'.

    If you put them in the outer query vars array, you will get the expected results:

    $pagesorter = array(
    	'posts_per_page' => -1,
    	'tag' => 'resource',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'terms' => array( $cat, $topic ),
    			'field' => 'id',
    			'operator' => 'AND'
    		),
    	)
    );

    Also, as you can see, you can put both category ids in one inner tax query.

  3. shagen
    Member
    Posted 3 years ago #

    That is like super hero stuff and it works perfectly! Thanks for the time.

Topic Closed

This topic has been closed to new replies.

About this Topic