WordPress.org

Ready to get started?Download WordPress

Forums

Query Posts Argument Selection (3 posts)

  1. Farzanmc
    Member
    Posted 1 year ago #

    Hi.

    This is what I want to do :
    Imagine that I have these categories for my posts.

    Color
    - Red
    - Black
    - White

    Year
    - 2011
    - 2012
    - 2013

    Now I want to select "red and white" posts that are 2012.

    Logically all the post that re "red and 2012" or "white and 2012".

    I tried cat , category__and and category__in , but no lock

  2. vtxyzzy
    Member
    Posted 12 months ago #

    I think you can use a tax_query to do that. I have not tested this, but it should be close:

    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		'relation' => 'AND',
    		array(
    			'taxonomy' => 'category',
    			'field' => 'slug',
    			'terms' => array( 'red', 'white' )
    		),
    		array(
    			'taxonomy' => 'category',
    			'field' => 'slug',
    			'terms' => array( '2012' ),
    			'operator' => 'IN'
    		)
    	)
    );
    $query = new WP_Query( $args );
  3. Farzanmc
    Member
    Posted 12 months ago #

    Thank you vtxyzzy.
    So far so good. The first test returned the right results. Although I didn't use 'operator' => 'IN' , but no difference.
    I'll try to expand my code with different options and then put it here for anyone who has the same problem.
    Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic