You don’t, the official line from the WordPress developers was that custom post types weren’t intended to do the job of normal ordinary posts, and that if you need post archives of dates etc, then you’re not doing things correctly, and you’re better off using post formats etc..
Custom post types are intended for web applications etc, whereas doing something such as setting up a custom post type that acts as a secondary or parallel blog with a different name, e.g. blog vs news, with the same abilities, is not what the feature was intended for, and would mean other technical issues arising from its implementation.
If you are still insistent on this, and simply using custom taxonomies and post formats is not enough, you could add rewrite rules in functions.php and redirect year/month archives in certain URLs to the post archive page, and then check on the custom post archive page if you’ve specified variables in your rewrite rules and load a different template, making sure in your rewrite rules to set the appropriate values.
EDIT -> while this answer still works for < WP4.4, since 4.4 support for Custom Post Types is now included in wp_get_archives()
There finally is a simple, quick and easy solution for date based archives of Custom Post Types in WordPress! This has been a long standing issue that is recorded here in the WP Core Trac.
It has yet to be solved but one of the contributors to the Trac has posted a simple plugin in GitHub that will enable you to have date based archives for CPTs.
After installing this plugin, or adding the code for your functions manually you case use archives for CPTs as such:
Note this new function wp_get_archives_cpt works the same as the standard wp_get_archives so you can use any of the regular arguments it accepts. However, it simply adds the ability for you to be able to add a custom post type name argument.
Not enough reputation to add this to taiken’s answer sorry.
However wanted to add that his answer did work for me, however the links were in the ‘localhost/date/2010’ format. Whereas I needed ‘localhost/postslug/2010’ format. I was able to fix this by using a string replace on the output of wp_get_archives.
So depending on how your permalinks are set this code will fix the 404 problem and redirect the links to the custom post type permalink structure:
$yearly_archive = wp_get_archives(array( 'type' => 'yearly', 'post_type' => '<your post type name>', 'echo' => '0') );
$blog_url = get_bloginfo('url');
echo str_replace(($blog_url . '/date'), ($blog_url . '<your post type slug>'),$yearly_archive);
Yes, you can.
All you need is make a filter for
wp_get_archives();
so it acceptspost_type
parameter:then call this:
Whenever you want to display archive by custom post type, just pass the post_type args:
You don’t, the official line from the WordPress developers was that custom post types weren’t intended to do the job of normal ordinary posts, and that if you need post archives of dates etc, then you’re not doing things correctly, and you’re better off using post formats etc..
Custom post types are intended for web applications etc, whereas doing something such as setting up a custom post type that acts as a secondary or parallel blog with a different name, e.g. blog vs news, with the same abilities, is not what the feature was intended for, and would mean other technical issues arising from its implementation.
If you are still insistent on this, and simply using custom taxonomies and post formats is not enough, you could add rewrite rules in functions.php and redirect year/month archives in certain URLs to the post archive page, and then check on the custom post archive page if you’ve specified variables in your rewrite rules and load a different template, making sure in your rewrite rules to set the appropriate values.
EDIT -> while this answer still works for < WP4.4, since 4.4 support for Custom Post Types is now included in
wp_get_archives()
There finally is a simple, quick and easy solution for date based archives of Custom Post Types in WordPress! This has been a long standing issue that is recorded here in the WP Core Trac.
It has yet to be solved but one of the contributors to the Trac has posted a simple plugin in GitHub that will enable you to have date based archives for CPTs.
After installing this plugin, or adding the code for your functions manually you case use archives for CPTs as such:
<?php wp_get_archives_cpt( 'post_type=custom_post_type' ); ?>
Note this new function
wp_get_archives_cpt
works the same as the standardwp_get_archives
so you can use any of the regular arguments it accepts. However, it simply adds the ability for you to be able to add a custom post type name argument.Not enough reputation to add this to taiken’s answer sorry.
However wanted to add that his answer did work for me, however the links were in the ‘localhost/date/2010’ format. Whereas I needed ‘localhost/postslug/2010’ format. I was able to fix this by using a string replace on the output of wp_get_archives.
So depending on how your permalinks are set this code will fix the 404 problem and redirect the links to the custom post type permalink structure:
Can’t add to takien’s post so here’s what I ended up having to do:
functions.php
Calling wp_get_archives()