5

In the sidebar of this page: http://lifebridgecypress.org/our-people, I have a list of upcoming events using this code...

<ul id="upcoming-events">
<?php
    $latestPosts = new WP_Query();
    $latestPosts->query('cat=3&showposts=10');
?>
<?php while ($latestPosts->have_posts()) : $latestPosts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

The only problem is that these events have already past... ha ha. I'd like to configure the code where it only shows upcoming events in the future, and then 24 hours after the event, it would disappear from this upcoming events list in the sidebar.

Does anyone know how to do this by modifying this code?

5
  • I'm writing this as I'm also writing an answer, but are you not using a Custom Post Type like 'events'? If not, why not? Dec 29, 2010 at 1:53
  • Hey Mike, there's an Event module in the backend using Flutter, whereby the admin can create a new event, or manage events that have already been set up. It's setup just like a post, but there are some custom fields setup through Flutter below the textarea where they can select date of the event, contact person, etc. In the category box, the admin selects Event, and it does it that way.
    – Spencer B.
    Dec 29, 2010 at 2:10
  • What is the post_type for these records in your database? I fear they are 'post' which means the logic you will need to display Events differently from regular Posts will be more complex. Is Category 3 a category you are using to specify that a Post is an event? If you don't understand Custom Post Types, read this: wordpress.stackexchange.com/questions/128/#187 Dec 29, 2010 at 5:08
  • Also Flutter hasn't been updated since v2.8 and thus evidently doesn't use Custom Post Types which is really want you need. Can you switch to another plugin? You can find a good list of them here: bit.ly/wp-cpt-plugins Dec 29, 2010 at 5:16
  • You might also find this support thread insightful: wordpress.org/support/topic/… Dec 29, 2010 at 5:19

2 Answers 2

3

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();
4
  • So Mike, I just put this into my functions.php file... and then what do I put in the sidebar where I want the upcoming events to show up?
    – Spencer B.
    Dec 29, 2010 at 3:29
  • @Spencer B. - IF you are using an "event" custom post type then you'd use your code from your answer and change the query from latestPosts->query('cat=3&showposts=10') to latestPosts->query('post_type=event&showposts=10'). (Question: A you a PHP developer or do you just cut & paste & pray? Knowing this helps me understand how to help you.) Dec 29, 2010 at 5:12
  • Not a PHP developer... cut and paste and pray, I guess. Slowly starting to understand it though... slowly.
    – Spencer B.
    Dec 29, 2010 at 5:23
  • @Spencer B. - Then definitely read up on the Custom Post Types. If at all possible you should switch from Flutter to using them and a plugin that lets you access the custom fields you have defined in Flutter. Dec 29, 2010 at 5:40
0

This code will only show your latest posts. One solution is to unpublish the posts regarding events that have already past.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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