4

I'd like to make the homepage accept query vars for year, month and date while still keeping the custom permalink structure for the rest of the site.

Basically the homepage needs to display several different links that are managed via custom posts.

So my hope is that http://mysite.com - will display the links set for the current day but then also enable http://mysite.com/2013/08/10/ - will display the links associated from that day as a type of archives. I need these as variables so I can do several different queries to display the relevant info.

Is this even possible, or am I attacking this the wrong way? Will this mess up other pages trying to use the permalink structure?

I've attempted to set my own query vars

    add_filter( 'query_vars', 'wpse12965_query_vars' );
function wpse12965_query_vars( $query_vars )
{
    $query_vars[] = 'a_year';
    $query_vars[] = 'a_month';
    $query_vars[] = 'a_day';
    return $query_vars;
}

add_rewrite_rule(
     '^/([^/]*)/([^/]*)/([^/]*)/?',
     'index.php?page_id=5&a_year=$matches[1]&a_month=$matches[2]&a_day=$matches[3]',
     'top');

My vars are recognized but it always redirects to the 404 not found page. Any suggestions would be greatly appreciated.

Edit So anyone else who may be checking this out I'll just mention that using the date.php template sorted this out and then I am easily able to retrieve the needed date vars like so

$day = (get_query_var('day'));
$year = (get_query_var('year'));
$monthnum = (get_query_var('monthnum')); 
2

The WordPress Template Hierarchy already supports that:

// Template Name
date.php

Then there's wp_get_archives() where you can set the according args.

WP Template Hierarchy

9
  • Nice one! I had no idea about being able to use date.php template. Nice new tool in the toolbox thank you.
    – user892670
    Aug 14 '13 at 1:00
  • Actually jumped the gun just a bit here. The date.php will only show if there is a post created on that date. It doesn't seem to register the dates that my custom post types were created on. I set has_archive to true and tried to republish all posts but still no dice.
    – user892670
    Aug 14 '13 at 1:34
  • You could as well use the custom post type archive template. Queries are ordered by date and the rest should be solvable using above mentioned function.
    – kaiser
    Aug 14 '13 at 1:58
  • Nope can't seem to get it to register on the archive-customPostName.php template at all either. Tried 3 different custom post types. Flushed the permalinks etc as well. Something else must be keeping these from being registered, but honestly have no idea what it could be.
    – user892670
    Aug 14 '13 at 3:06
  • Check your rewrite rule. You're basically rewriting everything to 'index.php?page_id=5... etc.
    – kaiser
    Aug 14 '13 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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