When your website means business.

«Return to Blog List How to List Upcoming Events and Events Archive with Custom Post Types

Recently, I had an opportunity to build an events listing that showed only upcoming events, with the next event appearing at the top. That’s pretty easy to accomplish. But I also wanted past events to disappear from the listing and show up instead on an events archive listing. It took a lot of searching and asking questions in two WordPress forums to piece together a query that worked.

First, I created a custom post type for events. Then I created a custom field called “order-date” to control ordering. Why a custom field? Why not rely on WordPress’ innate order-by-date function? Because WordPress uses the date posted, and it’s quite likely that events will not be posted in the order of their occurrence. But just as important, I needed the order-date to be the date of the event so we could use it to drop an event from the upcoming events listing and move it to the events archive listing. The custom field uses the Y-m-d date format for comparison with the Y-m-d format of today’s date.

Here is the query on the upcoming events listing template:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
       $today = date('Y-m-d', strtotime('-6 hours')); //define "today" format; note timezone offset of -6 hours
       query_posts(array(
       'post_type' => 'events', //query "events"
       'posts_per_page' => 5,
       'paged' => $paged,
       'meta_key' => 'order-date',
       'orderby' => 'meta_value',
       'order' => 'ASC', //sort in ascending order
       'meta_query' => array(
          array(
          'key' => 'order-date',
          'meta-value' => $value, //value of "order-date" custom field
          'value' = $today, //value of "today"
          'compare' => '>=', //show events greater than or equal to today
          'type' => 'CHAR'
          )
       )
    ));
    if (have_posts()) :
    while (have_posts()) : the_post();

On a separate template for displaying the archive listing, I made the following changes (commented) to display past events in descending order:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
       $today = date('Y-m-d', strtotime('-6 hours'));
       query_posts(array(
     'post_type' => 'events',
     'posts_per_page' => 5,
     'paged' => $paged,
     'meta_key' =>'order-date',
     'orderby' => 'meta_value',
     'order' => 'DESC', //descending order instead of ASC
     'meta_query' => array(
          array(
         'key' =>'order-date',
         'meta-value' => $value,
         'value' => $today,
         'compare' => '<', //show events less than today (past) instead of greater or equal
         <pre>'type' => 'CHAR'
          )
       )
    ));
    if (have_posts()) :
    while (have_posts()) : the_post();

Tags: , ,

Comments are closed.

Menu
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Cookies Notice