WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Yearly archives for custom post types (8 posts)

  1. van-knowmad
    Member
    Posted 2 years ago #

    I have found out that I do not need a plugin to get archive pages for my custom post type "news". I just copied archive.php from my theme into a archive-news.php and modified it. So, now I can visit mydomain.com/news and get a paged listing of all news items.

    But even after reading several old related threads (such as http://wordpress.org/support/topic/archive-list-and-page-for-custom-post-types-mysql?replies=9 and http://wordpress.org/support/topic/custom-post-type-tagscategories-archive-page?replies=40, I still cannot get something like mydomain.com/news/2011 to work.

    What's more I can't even dump the query string, because when I use a URL like that, it goes to my 404 error page.

    Is there a way to do yearly custom post archives without a plugin? I have tried adding the custom function from the older threads above, but nothing works, and those topics are closed.

    Any help would be greatly appreciated.

  2. kingstringy
    Member
    Posted 2 years ago #

    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?

  3. ifdion
    Member
    Posted 2 years ago #

    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.

  4. van-knowmad
    Member
    Posted 2 years ago #

    @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"

  5. ifdion
    Member
    Posted 2 years ago #

    "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

  6. van-knowmad
    Member
    Posted 2 years ago #

    @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/

  7. van-knowmad
    Member
    Posted 2 years ago #

    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

  8. Adrian Toll
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic