1

$args = array(
    'post_type' => 'wr_event',
    'posts_per_page' => -1,
    'meta_key' => 'event_date',
    'orderby' => 'meta_value_num',
    'order' => $order,
    'meta_value' => $yesterday,
    'meta_compare' => $compare,
    if ( is_null($cat) )
        'tax_query' => array(
            array(
                'taxonomy'  => 'event_type',
                'field'     => 'slug',
                'terms'     => $cat,
                'operator'  => 'IN'
                ),
           )
);

I'm confused, what do I pass in order to get all posts from all terms? Right now I can for instance pass sports in order to get all sports-events, but how do I get all events? I know I could just get rid of the tax_query() however I'm using this as a param, where I can pass along a certain tax or if not, i want all posts.

4

Simply omit (or not add) tax_query part of arguments.

$args = array(
    'post_type' => 'wr_event',
    'posts_per_page' => -1,
    'meta_key' => 'event_date',
    'orderby' => 'meta_value_num',
    'order' => $order,
    'meta_value' => $yesterday,
    'meta_compare' => $compare,
);

if ( ! is_null($cat) )
  $args['tax_query'] =array(
            array(
                'taxonomy'  => 'event_type',
                'field'     => 'slug',
                'terms'     => $cat,
                'operator'  => 'IN'
                ),
           );
  • I don't think this addresses the OP, which wants to query all posts that are assigned a term in a given taxonomy, without explicitly specifying all terms in that taxonomy. – Chip Bennett Jan 13 '14 at 16:46
  • @ChipBennett not clear, it does say that getting rid of it is acceptable, OP can always clarify :) – Rarst Jan 13 '14 at 16:49
  • Your method is correct (only adding the tax_query conditionally), but the underlying question - how to get all posts in a certain taxonomy is the meat of the question. :) – Chip Bennett Jan 13 '14 at 17:24
  • Thank you, updated my question … I get the following error if I do this … Parse error: syntax error, unexpected 'if' (T_IF), expecting ')' – mathiregister Jan 14 '14 at 10:32
  • @mathiregister this is pseudo-code, you need to adapt logic to your actual code – Rarst Jan 14 '14 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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