0

I am using a plugin called events manager. There is a custom post type called events, & each event has a date in a custom field. There is also a custom taxonomy called event categories.

On my site, all the events that have been assigned to a term in the event categories taxonomy, have an archive page. My code below shows these events on the archive pages. However, it also shows the events that have already passed, I only want it to show future events. How can I change my code to make this bit work, please?:

<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $termst = $term->slug; 
$today = getdate (); 
$args = array(
  'post_type' => 'event',
  'tax_query' => array(
    array(
      'taxonomy' => 'event-categories',
      'field'    => 'slug',
      'terms'    => $termst,
            'posts_per_page' => -1,
      'meta_key' => '_event_start_date', 
      'meta_query' => array( array( 'meta_key' => '_event_start_date' , 'meta_value' => $today, 'compare' => '>=' , 'type' => 'date')),
      'orderby' => 'meta_value'
          ),
  ),
);
$query = new WP_Query( $args );
   if($query ->have_posts()) : 
      while($query ->have_posts()) : 
         $query ->the_post();
 ?>
         <h1><?php the_title() ?></h1>
      <?php   echo ' - '. get_post_meta($id, '_event_start_date', true)    ?>
      <?php
      endwhile;
   else: 
?>
      Oops, there are no posts.

<?php
   endif;
?>
1

If you store the event start date in _event_start_date, then you can check the value of this field against current date:

if (strtotime(get_post_meta($id, '_event_start_date', true)) > time()){
    // future event
} else {
    // past event
}
0

If you want to show only the events with a start date in the future, ordered by closest date to current date, then this should be useful for you:

          $current_date = date('Y-m-d'); //Get the current date in the same format as date fields normally store in db
          $args = array(  
                  'post_type' => 'event',
                  'posts_per_page' => 4, // Number of event posts you want to query                                
                  'meta_key' => '_event_start_date',
                  'orderby' => '_event_start_date',
                  'order'  => 'ASC', // Order posts starting with the closest date to current date

                  'meta_query' => array(
                      array(
                          'key' => '_event_start_date',
                          'value' => $current_date,
                          'compare' => '>=', //Compare $current_date with _event_start_date and show only the post with a date after or equal $current_date
                                    'type' => 'DATE',
                                ),

                            )
                        ); 
                    $query_eventos = new WP_query($args);

I'm using this query to show some info of the event posts in the homepage of a WordPress site I'm creating for a client, so, this should work.

I'm not using a plugin, insted I created a custom post type for events, so, check the "meta_key", "post_type", "order_by" and "key" values.

0

You can use date_query within your meta-query, like this:

'date_query' => array(
    array(
        'column' => 'event_start_date',
        'after'  => 'now'  
    )
)

https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

0

For anyone who also arrived here in search of a way to query future events with wp_query, the below code works.

$arguments = array('post_type' => 'event', 'meta_query' => array(array('key' => '_start_ts', 'value' => time(), 'compare' => '>=')));
$query = new WP_Query($arguments);
  • Hi Luke, welcome to WPSE! Would you mind adding some explanation to your answer of why your code works/what it does? We prefer not to be a copy/paste service but to help people understand how to write better code themselves :) – Tim Malone Apr 28 '16 at 19:47

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.