I’ve got an “Event” custom post type. Each post has a meta field for the actual event date, which cannot be the posts’s publish date, as most all the event dates will be in the future.
With that, I’m trying to create an archive (year and month) for past and future events based on the meta field. Just can’t wrap my head around how I would create a query for this and have it display like url.com/post-type/2011/01
The meta field date value is set as YYYY-MM-DD.
Any help with this one is greatly appreciated! Thanks.
I did something similar for a client a while back, I’ll give you some of the code here as-is that you can possibly adapt to your needs. I’ll warn you, it’s quite a bit to parse through!
First, I set up some custom rewrite rules to get the year/month URL structure and some query vars to pass the year and month to my template. In this example I have a page set up with the slug
event-calendar
that I use as my events list / archive page. This code would go infunctions.php
:This is the code in my
page-event-calendar.php
template to query for events based on the year and month (if it’s set), otherwise I show just upcoming events:To output an archive list of years / months for my past events, I do some custom SQL to get all unique meta values for the
event_date
key. It’s quicker than doing a join with the actual posts, but the downside is you’ll get meta values for possibly unpublished posts. This was ok for my client’s needs, but possibly not yours.You could remove the
AND DATE(meta_value) < DATE(NOW()
condition if you want future events in the list as well.ok, that’s it! Hope you find some of this useful.
Your URL display won’t work like that unfortunately…not without a good amount of shoehorning at least.
Here’s the portion of the query you’ll need, this is untested, but should work, worst case you can store event data as a unix timestamp and convert back out when you go to display.
You can do the URLs in a less pretty manner and have it be example.com/post_type/?month=february pretty easily though. Hope this helps some.