0

I'm confused, what do I pass in order to get all posts from all terms? Right now I can for instance pass featured in order to get all 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.

(what I want, is if there is an event with a tag (featured) that event is shown, if there is no event with the tag (featured), show all events)

$now = date('d-m-Y H:i:s');
$cat = 'featured';
$args = array(
'post_type' => 'events',
'posts_per_page' => 4,
'numberposts' => 4,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'tax_query' => array(
  array(
      'taxonomy'  => 'events_tag',
      'field'     => 'slug',
      'terms'     => $cat,
      'operator'  => 'IN'
    ),
),
'meta_query' => array(
  'relation' => 'OR',
    array(
      'key' => 'start_date',
      'value' => date('Ymd', strtotime('now')),
      'type' => 'numeric',
      'compare' => '>=',
    ),
    array(
      'key' => 'end_date',
      'value' => date('Ymd', strtotime('now')),
      'type' => 'numeric',
      'compare' => '>=',
    ),
 ),
if (! is_null($cat))
  'tax_query' => array(
         array(
           'taxonomy'  => 'events_tag',
           'field'     => 'slug',
           'terms'     => $cat,
           'operator'  => 'IN'
        ),
  ),
);
$wp_query = new WP_Query($args);
0

I would start by reorganizing your $args code to make more sense.

$now = date('d-m-Y H:i:s');
$cat = 'featured';
$args = $cat_args = array(
    'post_type' => 'events',
    'posts_per_page' => 4,
    'meta_key' => 'start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        'relation' => 'OR',
        array(
           'key' => 'start_date',
           'value' => date('Ymd', strtotime('now')),
           'type' => 'numeric',
           'compare' => '>=',
        ),
        array(
           'key' => 'end_date',
           'value' => date('Ymd', strtotime('now')),
           'type' => 'numeric',
           'compare' => '>=',
        ),
    ),
);

// Set the $cat_args array to the category tax_query
$cat_args['tax_query'] = array(
                            array(
                              'taxonomy'  => 'events_tag',
                              'field'     => 'slug',
                              'terms'     => $cat,
                              'operator'  => 'IN'
                            ),
                        );

if ( get_posts( $cat_args ) ) {    // See if any category posts exist
    $query_args = $cat_args;
} else {                           // Otherwise, pass original $args
    $query_args = $args;
}

// Use the newly set $query_args
$wp_query = new WP_Query($query_args);
  • Thank you, but does not show all events (post), I made a print_r ($ wp_query), and I see that it still shows the tax_query, I do not understand why – Juan David Oct 5 '17 at 15:12
  • Well, you are setting the $cat from the beginning, do you have any posts under that category? – socki03 Oct 5 '17 at 15:13
  • there is no event with $cat (featured) – Juan David Oct 5 '17 at 15:14
  • Ok, so I think I understand what you're asking a bit better now. You want 4 total posts to show up, but you want any featured to show up first, and then pull the number necessary for the remainder? – socki03 Oct 5 '17 at 15:15
  • what I want, is if there is an event with a tag (featured) that event is shown, if there is no event with the tag (featured), show all events – Juan David Oct 5 '17 at 15:17

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.