Events Lists in WordPress.

March 16, 2013

So you want to make an events list in WordPress? There is a lot to do! First off lets define how this should work. Since I’m writing this blog I’ll tell you how mine works and then you decide if it’s how you want YOURS to work.

  1. It’s going to be a dynamic list.
  2. It’s going to pull events from an Event content type.
  3. It’s going to order them by date, newest on top.
  4. It’s going to display all the info for other people.

Ok so what do we need to set this up?

First find a way to create content types. I use the Custom Post Type UI plugin. This is a very good easy to use plugin that will do all that you need it to do.

Secondly, we need to be able to add specific fields into the post type. I strongly recommend using Advanced Custom Fields plugin. The main reason for this is that you are going to want to Repeater Fields and inside those store all your info, including a “date” input type.

Ok so create your custom field types inside your posts. Set a date as an Advanced Custom Field and remember where you put it.

Once you have a good list in the database it’s as simple as Querying.

 

<? if ( get_query_var('events') ) $paged = get_query_var('events');
$query = new WP_Query( array( 'post_type' => 'Events', 'paged' => $paged, 'orderby' => 'meta_value','meta_key' => 'date', 'order' => 'ASC', 'posts_per_page' => -1 ) );
$now = date("Ymd");
?>

Ok so you will notice a few things about this code.

  1. I use an orderby metavalue command.
    This is so that we can order by date. Advanced Custom Fields will have all it’s fields available as metadata so you can orderby          different things. In our case it’s a date.
  2.  I created a “date” variable called “now”.
    This is so I can filter out events that already happened from appearing to the user. It’s incredibly important to have the format be YMD so that you can do straight data comparisons.

So now that we have the query set up we are all but ready to start incorporating it into our theme. All we need to know is what to print from what is being queried.

 

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

<? $date = DateTime::createFromFormat('Ymd', get_field('date')); ?>
<? if($now > $date->format("Ymd")) {  }else{ ?>

Ok so now you notice how we make the custom field “date” into an object named date and we check to see if “now” is greater than “date” who is also formatted as YMD.

This is going to call all events that are happening anytime farther that right at this very millisecond.

After this build your content any way you wish and watch the magic happen!

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive