Viewing 7 replies - 1 through 7 (of 7 total)
  • I was just about to post the exact same question. I can display CPTs on a page and/or make a simple archive page of ascending or descending custom posts by copying and renaming the archive template, but when I try to make a yearly archive, I either get regular blog posts (non-CPT) or 404.

    I even tried using the Custom Post Types Archives plugin, and that generated links for the years, but the links went 404 as well. I tried all the fixes for the plugin and had no luck, hence my interest in a non-plugin-related solution. Also it *seems* like it should be a pretty basic thing to do.

    Anyone have any info?

    Have you tried a custom wp_query on a page template?
    That way you can call the archive like mydomain.com/news-archive/?year=2011.

    Thread Starter van-knowmad

    (@van-knowmad)

    @kingstringy: I think I tried that plugin, but I couldn’t get the links to come out right.

    I agree that it is so basic, there should be a How-To for it in the Codex.

    @ifdion: If I use your method, what would the page and file structure look like? Right now we are grouping Blog Posts, News, and Events under one custom page called “The Latest” with a custom template. Each one of those has a custom sub-page that lists the most recent from that type of post with links to archives (they share a custom template).

    latest/blog
    latest/news
    latest/events

    Would I then need sub-pages like, “latest/news-archive” or would it be “latest/news/archive/?year=2011” ?

    Ideally, I’d like them archives to look consistent, something like “/latest/news/2011/” or “latest/news/archive/2011

    “The Latest” page should be easy. get_posts or WP_query will do the trick.

    For the sub pages, you can use a conditional statement to add a time parameter to the loop, if theres a $_GET[‘year’]. Doing so will make ‘blog.com/news/?year=2011’ display the latest post in 2011.

    Hope this help

    Thread Starter van-knowmad

    (@van-knowmad)

    @ifdion, Thanks for your suggestions.

    I saw something similar that worked in this thread: http://wordpress.org/support/topic/archive-list-and-page-for-custom-post-types-mysql?replies=2, but I was trying not to create even more custom pages than I already had.

    I finally found a way to use WordPress-style archive templates by adding these lines to our theme’s functions.php file:

    // Add custom rewrite rules to handle things like years in custom post archives
    function add_rewrite_rules($aRules) {
        $aNewRules = array(
            'news/([0-9]{4})/page/?([0-9]{1,})/?$' => 'index.php?post_type=news&year=$matches[1]&paged=$matches[2]',
            'news/([0-9]{4})/?$' => 'index.php?post_type=news&year=$matches[1]',
            'event/([0-9]{4})/page/?([0-9]{1,})/?$' => 'index.php?post_type=event&year=$matches[1]&paged=$matches[2]',
            'event/([0-9]{4})/?$' => 'index.php?post_type=event&year=$matches[1]'
        );
        $aRules = $aNewRules + $aRules;
        return $aRules;
    }
    
    // hook add_rewrite_rules function into rewrite_rules_array
    add_filter('rewrite_rules_array', 'add_rewrite_rules');

    With these new rules, if I use URLs like /news/2011, WordPress will use my custom template archive-news.php, which I based on archive.php, to show a yearly archive of news articles.

    This article were a great help in figuring this out:

    http://www.dev4press.com/2012/tutorials/wordpress/practical/debug-wordpress-rewrite-rules-matching/

    Thread Starter van-knowmad

    (@van-knowmad)

    P.S. I forgot to mention this article, which got me started on the right track, and helped me figured this out:

    Passing Query String Parameters in WordPress URL

    Thanks van-knowmad – this works perfectly.

    One extra thing that people might want to include is a conditional call to flush_rules() if the rules haven’t been included yet. This means that you don’t have to click the Save button in Settings > Permalinks when you change the rewrite rule. See the easy example in the WP_Rewrite page on the Codex.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Yearly archives for custom post types’ is closed to new replies.