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.

This seems trivial but I can't get it to work. Here's my code:

        $args = array(
            'numberposts'     => -1,
            'eventcategory'   => 'nice-events',
            'post_type'       => 'event',
            'post_status'     => 'publish' 
        );

        var_dump(get_posts($args));

As you can see, my taxonomy is eventcategory and the term is nice-events. There are actually over 50 terms and regardless of what term I use in place of nice-events I always get the same result: all of the posts. So the term is being ignored and I have no idea why.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You cannot make up your own arguments - rather than replacing the 'category' argument with your taxonomy name, use 'tax_query'.

See "taxonomy parameters" section of the codex on get_posts.

$args = array(
    'post_type' => 'event',
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'eventcategory',
            'field' => 'slug',
            'terms' => 'nice-events'
        )
    )
);


$your_query = get_posts( $args );

// do something with $your_query

Alternatively, you could make use of the WP_Query class:

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'event',
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'eventcategory',
            'field' => 'slug',
            'terms' => 'nice-events'
        )
    )
);


$your_query = new WP_Query( $args );

// do something with $your_query
share|improve this answer
    
@kaiser Indeed, you can. I was under the impression get_posts() did not support tax_queries. You're right, codex says it does. –  Johannes Pille May 3 '12 at 14:24
    
I am not getting any result using your code.. –  jilseego May 3 '12 at 14:36
    
@jilseego There was a misplaced comma in it - the snippet should now be ready for copy & paste... –  Johannes Pille May 3 '12 at 14:40
    
Yah, I corrected that already before you did. Didn't get any result, really. –  jilseego May 3 '12 at 14:42
    
The first one gets empty array. The second one returns this: pastebin.com/wnGE88Cn, which I don't think it's correct(?). –  jilseego May 3 '12 at 14:56

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.