WordPress 3 custom post type date based archive

August 17, 2011 |

We needed to implement this for our great client Seattleite.com.  We built a Events system for them using custom post types (CPT) but needed to create a date based archive so that someone could click on a calendar day and be taken to that day and see all events for that day.  This solution is geared around an events custom post type but could work with many other types, using the post-date instead of a custom field.

First make sure that you have a custom post type setup, next you will need to make sure you have the

[[code]]czoyNDpcIlxcXCdoYXNfYXJjaGl2ZVxcXCcgPT4gdHJ1ZSxcIjt7WyYqJl19[[/code]]

set where you instantiate the CPT in functions.php.

Second, make sure you have a archive-CustomPostTypeName.php  ex: archive-event.php (for a event custom post type).  Next you need to add the code from this post to your functions.php.

Next go to your template file where you want to use this calendar feature and insert the following code:
<!--?php echo ucc_get_calendar( $post_types = array('event')); ?-->
This template tag cals the calendar.

Finally we wanted the events to use a custom url structure so that I could use the archive template I had made.  If you want this as well, you will need to paste the following code into your functions.php template.


//  Begin Rewrites
add_rewrite_tag('%event-date%','([\d]{4}-[\d]{2}-[\d]{2})');

add_action( ‘init’, ‘event_rewrites’ );
function event_rewrites()
{
add_rewrite_rule( “events/day/([\d]{4}-[\d]{2}-[\d]{2})/?$”, ‘index.php?post_type=event&event-date=$matches[1]‘, ‘top’ );
}
add_filter( ‘query_vars’, ‘event_vars’ );
function event_vars ( $vars )
{
$vars[] = ‘event-date’;
return $vars;
}
// End Rewrites

This sets up the urls to be: http://www.YOURURL.com/events/day/2011-08-05

The last thing that had to be done was to slightly modify one line of the code from the other post to fit our url structure:
$calendar_output .= '

This makes sure that the link it generates will be to the url we created.

Leave a Reply