I've got an archive that's displayed using a URL on the form www.mysite.ex/mycpt and I need to make a rewrite rule for that URL which redirects to a permalink with the latest date archive, which is on the form www.mysite.ex/mycpt/2012/01/31. The archives use the same archive-mycpt.php template, but my guess is that the fact that it is a Custom Post Type does not matter in this case.

I can get the most recent post date by using get_posts and extract the post_date for the post_type and use that info to attach a path to the above mentioned URL. I suppose I could put that in a rewrite rule, but how do I make a rewrite rule that always has up-to-date information on the most recent post date?

I think that I might need to make an action hook somewhere to update the rewrite rule when a new post of the post_type is made, but I'm not sure which hook to use and how to use it, or if it's a good approach to begin with.

Any ideas?

edit: Thanks to woony for pointing me in the right direction.


I don't think this is a good approuch. I think it's best to redirect your page at mycpt. let it get the last date in php using your get_posts. And let the page redirect to where you need to be. Don't go auto updating your .htaccess, that's not what it is made for.

  • Do I understand correctly that you suggest to redirect in the archive-mycpt.php template? Can that be done if the same template is used to display the date archives? Maybe a condition could be used. – seron Aug 17 '12 at 10:47
  • Yes, you could, if I understand your question correct. You can easily add any condition. Depending on your needs. Maybe you should try to make your initial question a little bit more clear. You want your archive to go to the last one yes? – woony Aug 17 '12 at 11:20
  • I did as you suggested and made a redirect in the archive template based on a condition. I'll post my solution below. – seron Aug 17 '12 at 14:52
  • alright. up me one if you like – woony Aug 17 '12 at 14:57
  • doing redirection within a template is a waste of resources, use an action hook before the database is queried, like parse_query or pre_get_posts. see action reference in codex. you also avoid headers already sent errors that will encounter if any output reaches the browser before redirection. – Milo Aug 17 '12 at 15:10

At the beginning of my Custom Post Type archive I added this code:

    if (!is_date()) { // redirect mycpt archive page to the most recent post date page


A also added this function to the functions.php file:

* Genrate a URL for the most recent mycpt post date.
function seron_get_most_recent_mycpt_link() {

    $latest_post = get_posts(array('numberposts' => 1,
                                   'orderby' => 'post_date',
                                   'order' => 'DESC',
                                   'post_type' => 'mycpt')

    $date = explode(' ', $latest_post[0]->post_date);
    return home_url() . '/mycpt/' . str_replace('-', '/', $date[0]);

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.