Only Showing Upcoming Events

Hi @Spencer B.:

Funny, my client submitted a bug ticket for the events modules I wrote for this very issue the other day, and I just fixed it a few hours ago.

Note that my example uses a Custom Post Type of 'event' which is very useful to be able to differentiate logic for Events distinct from Posts. If you must use regular Posts you’ll need to somehow identify the required logic what makes the pages that list Posts different from your page that lists Events.

You’ll need to use the 'posts_where' hook to filter out the Events whose dates are earlier than 24 hours ago. The hook function below tests to see if the query is for post_type="event"; if so it modifies the query to add a criteria to the SQL WHERE clause.

When you save a WordPress checks to see if it is a future date, and if so sets the post_status="future" rather than 'publish'; you need to correct that. You can use the 'wp_insert_post_data' hook to reset to 'publish' if WordPress has set to 'future'.

What follows is a class to encapsulate this logic, which you can copy into your theme’s functions.php file:

class Display_Future_Events {
  static function on_load() {
    add_filter('posts_where',array(__CLASS__,'posts_where'),10,2);
    add_action('wp_insert_post_data', array(__CLASS__,'wp_insert_post_data'),10,2);
    add_action('init', array(__CLASS__,'init'));
  }
  static function posts_where($where,$query) {
    if (self::is_event_list($query)) {
      global $wpdb;
      $yesterday = date('Y-m-d H:i:s',time()-(24*60*60));
      $where .= $wpdb->prepare(" AND post_date>'%s' ",$yesterday);
    }
    return $where;
  }
  static function is_event_list($query) { 
    // Logic here might need to be fine-tuned for your use-case
    if (is_string($query->query))
      parse_str($query->query,$args); 
    else
      $args = $query->query;
    return isset($args['post_type'])=='event';
  }
  static function wp_insert_post_data($data,$postarr) {
    if ($data['post_type']=='event' && // Will need more logic here for post_type="post"
      $postarr['post_status']=='publish' &&
      $data['post_status']=='future')
        $data['post_status'] = 'publish';

    return $data;
  }
  static function init() {
    register_post_type('event',
      array(
        'labels'          => self::make_labels('Event'),
        'public'          => true,
        'show_ui'         => true,
        'query_var'       => 'event',
        'rewrite'         => array('slug' => 'events'),
        'hierarchical'    => true,
        'supports'        => array('title','editor','custom-fields'),
        /*
         See more 'supports' options at
          http://codex.wordpress.org/Function_Reference/register_post_type
        */
      )
    );
  }
  static function make_labels($singular,$plural=false,$args=array()) {
    if ($plural===false)
      $plural = $singular . 's';
    elseif ($plural===true)
      $plural = $singular;
    $defaults = array(
      'name'               =>_x($plural,'post type general name'),
      'singular_name'      =>_x($singular,'post type singular name'),
      'add_new'            =>_x('Add New',$singular),
      'add_new_item'       =>__("Add New $singular"),
      'edit_item'          =>__("Edit $singular"),
      'new_item'           =>__("New $singular"),
      'view_item'          =>__("View $singular"),
      'search_items'       =>__("Search $plural"),
      'not_found'          =>__("No $plural Found"),
      'not_found_in_trash' =>__("No $plural Found in Trash"),
      'parent_item_colon'  =>'',
    );
    return wp_parse_args($args,$defaults);
  }
}
Display_Future_Events::on_load();

Leave a Comment