Build a Simple Event Listing in WordPress Without a Plugin

A simple way to create events in WordPress without a plugin

I had a task: add events (date, title, description, etc.) from the dashboard and output them in the right place on the site. At first I installed a plugin, the one with the highest rating, but it did not live up to my expectations. OK, no problem, we can do it with clean code.

Problem: Create events in WordPress without an events plugin, and show only upcoming ones.

Solution: Actually, we will still use one plugin, Advanced Custom Fields (ACF), but if you build WordPress themes, you probably use it anyway. First we register the post type “event”, then we output the events in a custom query sorted by date. Events whose date has already passed must be hidden.

Step 1. Register the post type

<?php
add_action( 'init', 'register_event_post_type' );

function register_event_post_type() {
    $labels = array(
        'name'               => __( 'Events', 'theme-name' ),
        'singular_name'      => __( 'Event', 'theme-name' ),
        'menu_name'          => __( 'Events', 'theme-name' ),
        'all_items'          => __( 'All Events', 'theme-name' ),
        'add_new_item'       => __( 'Add New Event', 'theme-name' ),
        'edit_item'          => __( 'Edit Event', 'theme-name' ),
        'view_item'          => __( 'View Event', 'theme-name' ),
        'search_items'       => __( 'Search Events', 'theme-name' ),
        'not_found'          => __( 'Not found', 'theme-name' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'theme-name' ),
    );

    register_post_type( 'event', array(
        'labels'        => $labels,
        'supports'      => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
        'show_in_rest'  => true,
        'public'        => true,
        'has_archive'   => false,
        'menu_position' => 6,
        'menu_icon'     => 'dashicons-calendar-alt',
        'rewrite'       => array( 'slug' => 'event' ),
    ) );
}

Then create an ACF field group for the “event” post type with a Date Picker field start_date.

Step 2. Output the upcoming events

The ACF Date Picker always saves the date in the database as Ymd (for example 20191015), whatever the display format is. So the dates can be compared and sorted as numbers right in the query, and past events are filtered out by the same query:

<?php
$query = new WP_Query( array(
    'post_type'      => 'event',
    'posts_per_page' => get_field( 'posts_per_page' ) ?: 10,
    'meta_key'       => 'start_date',
    'orderby'        => 'meta_value_num',
    'order'          => 'ASC',
    'meta_query'     => array(
        array(
            'key'     => 'start_date',
            'value'   => current_time( 'Ymd' ),
            'compare' => '>=',
            'type'    => 'NUMERIC',
        ),
    ),
) );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post(); ?>
        <article class="event">
            <time><?php the_field( 'start_date' ); ?></time>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt(); ?>
        </article>
    <?php endwhile;
    wp_reset_postdata();
else :
    $message = get_field( 'no_events_found_message' );
    echo '<p>' . esc_html( $message ? $message : __( 'Currently there are no events to show here', 'theme-name' ) ) . '</p>';
endif;

current_time() uses the time zone from the WordPress settings, so an event disappears after its day ends on the site, not on the server. posts_per_page and the “no events” message come from ACF fields of the page, with defaults if they are empty.

NOTE: In my first version I loaded all events, compared each date with strtotime() in PHP and excluded the past ones with post__not_in. It works, but it is two queries instead of one and gets slower as events pile up.