Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm struggling with what seems like should be so simple looking at the codex.

I have a custom post type, let's call it 'portfolio'.

A custom taxonomy for that, let's call it 'clients'.

I can use get_posts() to list all 'portfolio' items but also need the ability to pass in / filter by the 'clients' taxonomy slugs or display all. ("acme,ibm,tbs" - or null/blank)

I've torn apart my sample code so many times that I don't remember anymore what I haven't tried.

Maybe I should instead go with WP_Query()? Sorry for probably such a stupid question, I just can't interpret the example apparently.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

Both get_posts() and wp_query() should be able to do this by passing the tax_query array into them.

Example:

$query = array(
    'post_type' => 'portfolio',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'clients',
            'field' => 'slug',
            'terms' => array( 'acme' )
        )
    ),
    'orderby' => 'menu_order',
    'order' => 'ASC'
);

$posts = get_posts( $query );
share|improve this answer
    
Ah-ha! I only had one array in the tax_query, not the array of arrays. Thank you so much, my head was about to fall off... –  H. Hins Nov 17 '13 at 2:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.