0

I have an events cpt with a slug "event" I am using it in several places but I am trying to create a "day" view that shows all the events for a given day. I also have the archive arg set in the CPT creation in functions.php

I have been around and around on this one but essentially here is what I am trying to do:

domain.com/index.php?post_type=event&event-date=2011-07-25

this brings up a page that lists a whole bunch of crap(random posts), not the events for that day.

Also if I change it to event_archive.php?post_type=event&event-date=2011-07-25, I get a Page can't be found error. I am not even worried about custom rewrite rules at this point, I just want it to work.

1 Answer 1

2

You will have to use the rewrite API to get that to work. First you need to register the rewrite rule.

add_action( 'init', 'wpse23712_rewrites' );
function wpse23712_rewrites()
{
    add_rewrite_rule( 'events/day/(\d{4}-\d{2}-\d{2})/', 'index.php?post_type=event&event_date=$matches[1]', 'top' );
}

Then add your event_date to the query vars so wordpress understands it.

add_filter( 'query_vars', 'wpse23712_vars' );
function wpse23712_vars ( $vars )
{
    $vars[] = 'event_date';
    return $vars;
}

Then on the front end (in your archive-event.php) you can grab the event date with get_query_var() and use it in your query. I'd suggest you store something with a custom field (in the wp_postmeta table) for each even with the ISO formatted date. Then you can run a meta_query to retrieve those event posts.

So when someone navigates to yoursite.com/event/day/2011-07-28/ it will show events for that day.

I didn't test the above, but you should able to take it from here.

6
  • So if I understand you right then using this code in my functions.php, I should be able to use the following url structure: /event/day/2011-07-28 Then were you saying I should use the event-archive template to list out the day thats being queried, also I can use a normal WP_query to find all events where the date matches the get_query_var()? Commented Jul 22, 2011 at 20:32
  • Yes, you can put it functions.php. And sort of on the second part. When you construct a new wp query, you'll have to do something like this: $new = new WP_Query( array( 'meta_key' => 'iso_date', 'meta_value' => get_query_var( 'event_date' ) ) ); That would get all the posts that have the meta key (custom field value) that matches the date in the URL. Commented Jul 22, 2011 at 21:09
  • But you'll probably have to check to make sure that query variable is set first, and if it's not, do the normal query. Again, I'm not testing this stuff, so you'll have to putz around with it to get it perfect. Commented Jul 22, 2011 at 21:12
  • I think I get it now after readying up on get_query_var. My only question is how do I make it use the archive-event.php template instead of the index.php template? thanks for the help. Commented Jul 22, 2011 at 21:35
  • If you have an archive-event.php it will get used because of the ?post_type=event. WordPress will look for that template first, not index.php. Commented Jul 22, 2011 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.