If you’ve done any work with The Event Calendar from Modern Tribe, then there’s a chance you’re familiar with some different ways to go about doing certain tasks.
That is, programmatically creating events, altering events, setting up repeating events (if it’s the pro version), and so on.
What if, though, you need to go about querying event post types – some or all, doesn’t matter – and you find that your query isn’t working? And no, I’m not talking about using a raw SQL query or even a parameterized version with the $wpdb class.
I’m talking about WP_Query.
Querying Event Post Types
Here’s the short of it: If you attempt to use WP_Query in a way that you’re used to using it (that is, manually specifying the post type), then you’re likely not going to get any results. Which, of course, makes no sense especially when you can see them in WordPress and the database.
Instead, the arguments for the query need:
- several post types into account,
- several post statuses into account,
- and a custom array key unique to the plugin itself (as of version 3.0).
In short, an incredibly basic version of the arguments should look like this:
But here’s the thing: This isn’t a universal case. There are times where you may not specify the eventDisplay key and still get results. So what gives?
Always Read The Documentation
According to the documentation:
eventDisplay is used to indicate the general shape of the query – typically it can be one of upcoming, past or custom (and if you don’t specify anything it will assume you are interested in upcoming events)
So if you have an upcoming event, then you’re going to be fine, but if you want to grab everything that you’ve got in your installation, specify custom as your value.
The Bigger Picture
The reason I stumbled across this idiosyncrasy has more to do with with the implementation of creating events and associating them with a particular user account.
But I’ll go into the details of that in another post. For now, this is all about making sure we can go about querying event post types and get results with which we can work.