On a recent client project, I was needing a way to order events based, not on the actual published date of the post, but rather by the date of the event in WordPress.

The solution I (finally) found involves sorting the posts in the Events category based on the value of a certain custom field.

Before finding the solution though, I tried several different ways to get events to show up, ordered from newest to oldest., but nothing I came across worked completely.

Skip ahead to the solution…

What Didn’t Work

In order to save some people who are looking into doing something similar, I’ll tell you what didn’t work for me. Some of the things mentioned here might work for your project, but it didn’t work for me.

WordPress Plugins

I started out trying not to reinvent the wheel and just go with a WordPress plugin. However, I couldn’t find anything that fully met my needs.

Calendar Plugins

I tried using several calendar and event plugins to get the job done. These might work for you, but I needed to have each event have its own post page. This was because each event had a registration form for the individual events.

Most of the WordPress plugins I found, either didn’t allow linking to actual posts—they only showed a summary. Many of them used a non-WordPress system to store the details about the event. Some just didn’t work at all and gave errors.

Event Plugins

I had similar issues with WordPress event plugins as I did with the calendar plugins. They had their own system of storing events (not as posts) and many of them where too rigid as far as options.

Future Post Plugins

I also tried using a future post plugin to make it appear that posts that were scheduled for a future date (say the date of the event) were actually published.

All the plugins I found apply this to all posts in all categories with no way to control it. So if you ever scheduled any post in any category to publish later, it would be published immediately—not just your future events.

Other WordPress code

In the meantime, I tried different code offered on sites to help with this and some would work partially, while others would not.

One “solution” was to schedule the post to be published on the date of the event and then use some PHP/WordPress code to show all the posts tagged as future posts in a certain category.

I was excited about this and it looked like it was exactly what I wanted.

The flaw with this plan is that while it will show the future scheduled posts and they will be all in the correct chronological order on a page pulling in posts, you can’t actually view individual post unless you’re logged in as administrator. People not logged in will get a 404 File Not Found message when trying to view the full post. This is just the way WordPress is designed and I couldn’t find any workaround for this.

The Solution, Already

I know, a lot of build up for the solution. Well, here it is.

Part 1: Flutter Plugin and Custom Fields

FlutterI was already using a great plugin called Flutter, which lets you create custom write panels and work with custom fields and all in a much more user-friendly way.

I created an Events write panel in Flutter and made an event_date custom field. I used this to display the date of each event instead of the published date of the post on my main Events page.

Flutter’s documentation is pretty sparse, and I could not get the sort by custom field to work using the code examples on their site, so…

Part 2: Order Using a Custom Function

Since the custom fields made by Flutter are just regular WordPress custom fields, we don’t have to use Flutter’s code to order them.

I found a great post and bit of code on Think Press‘ site:

Order Posts by Custom Key Revisited

Take a look at the above bit of PHP code and make these adjustments:

  1. Wrap the code provided in PHP tags (see comments if you’re unsure how to do this)
  2. Copy and paste that code into your functions.php file in your WordPress theme.
  3. Adjust the if statement to match your page or category to your needs
  4. Change to the custom field you want  to sort by on this line (change ‘Price’ to your custom field):
    $where.= "AND $wpdb->postmeta.meta_key = 'Price' ";

This worked like a charm. All the events are ordered from newest to oldest—based on the event date, not the publish date. This also works on any custom field; not just those used in Flutter.

{openx:7}

Recap

Here’s a recap of my basic process:

  • Created an Events category in WordPress
  • Used Flutter to set up custom fields for the event date, time, etc
  • Set a custom single post template for posts in the Events category (to show event registration code)
  • Set up my Events page to pull in those posts
  • Added the code found at Think Press‘ site to my theme’s functions.php file
  • Replaced the post publish date with the custom field for the event’s date in my templates

Some of the above steps may not apply to your project, but hopefully this will save you some time when needing to sort by custom field in WordPress and when working with creating events.

Similar Posts: