Forums

[resolved] can you query posts assigned to two categories? (4 posts)

  1. panmac
    Member
    Posted 9 months ago #

    Hi - I'm still learning this so let me know if anything is unclear or if I'm using the wrong terminology.

    I'm wondering how one would go about querying for posts that are assigned to two categories.

    For example, I've set up a custom post type called 'resume_entry'. Each 'resume_entry' will be assigned to two custom taxonomy categories. For example, 'red' and 'apple'. If I have a bunch of categories, let's say: 'red', 'blue', 'apple', 'orange', 'banana' - I want to be able to pull all the posts that are assigned to the 'red' category AND the 'apple' category to display in my page template.

    Does any of that make sense? Currently I'm just querying for all the 'resume_entry' posts with WP_Query('post_type=resume_entry'); , but I want to specify within that type's meta_box fields.

    I've googled a lot and found some complicated answers (many involving wpdb and arrays) but I'm hoping someone will have the patience to explain a solution to me.

    Thank you!

  2. Jason
    Member
    Posted 9 months ago #

    You could do something like this:

    $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'your_custom_category',
    			'field' => 'slug',
    			'terms' => array( 'red', 'apple' )
    		)
    	)
    )
    $query = new WP_Query( $args );

    Reference: http://codex.wordpress.org/Class_Reference/WP_Query

  3. panmac
    Member
    Posted 9 months ago #

    Thanks, Jason! I had looked through that reference page, but couldn't understand. With a night's sleep and your suggestion I was able to work it out.

  4. Jason
    Member
    Posted 9 months ago #

    Awesome, no problem :)

Reply

You must log in to post.

About this Topic