Adding Custom Post Types To wp_get_archives()

Again with the custom post type extensions. This function lets the native WordPress `wp_get_archives()` [^1] [^2] know about public custom post types; just add it to the `functions.php` of your theme. Of note: the builtin `link` post type can be included at the `array_merge()` point; I just had no use for it.

add_filter( 'getarchives_where' , 'ucc_getarchives_where_filter' , 10 , 2 );
function ucc_getarchives_where_filter( $where , $r ) {
$args = array(
'public' => true ,
'_builtin' => false
$output = 'names';
$operator = 'and';

$post_types = get_post_types( $args , $output , $operator );
$post_types = array_merge( $post_types , array( 'post' ) );
$post_types = "'" . implode( "' , '" , $post_types ) . "'";

return str_replace( "post_type = 'post'" , "post_type IN ( $post_types )" , $where );

## References

[^1]: [Function Reference/wp get archives](
[^2]: [Custom post type permalinks](

16 thoughts on “Adding Custom Post Types To wp_get_archives()

  1. Ok, I’ve used the above code but I still can’t get yearly or monthly archives of my custom posts to work. Should they even work? Is there any way to do this?

  2. This code just makes sure that if a month has only custom post type posts, it is included in the archive listing. To include custom post types on the actual archive pages, you need to use something like this.

  3. Ok I see now. But what if I wanted to have archives for my custom post type separate from normal posts:

    like http://mysite/custom-type/2010

    You can get up the permalinks ok with plugins but I see no way of automatically generating a list the links to each year, because wp_get_archives just generates links to:


    Out of the scope what your plugin intends to do I know, but I’m at a loose end trying to do this. I guess I could use javascript to change the links and add the ‘custom-type’ bit.

  4. Hi, I found that your filter fixed the issue I was having with generating a list of months containing custom post types.

    However the URL that is generated for a month that only contains custom post types (and no default ‘posts’) such as “/2010/12/” defaults back to the homepage of the site, but the URL “/2010/11/” (which contains both default posts and custom posts) will utilize the date.php template file and list out all the posts as it should.

    If I create a default post, then the URL “/2010/12/” will work.

    Is there another hook that needs to be tapped into to modify something so that when the monthly archive URL format is called upon, it will also check to ensure that custom post types exist and not just default post types?

  5. I’m having the same problem as Paul in that new posts in the custom Post Type register to show up in the {month : year} listing from the archives but when I click on a {month : year} item in the list which only contains a post from the custom post type nothing is returned and it redirects to the error404 page.

    I’ve tried adding in the function you linked to in your reply above but that adds custom Post Types to the loop along with Page entries – the whole shebang! I don’t really want to go this far as I have two separate posting areas: News, and Staff News. I don’t want News to contain Staff News and vice versa so putting those results in the loop isn’t quite what I’m after. I also never want any Page entries in the loop.

    I just want the {month : year} list items in the archive containing entries from all Post Types (not Pages) and display results when clicking on them instead of returning to the error404. The URL is correct but nothing is returned:


  6. Can you describe that code a bit so I know what to change for my custom post types and what stays the same?

Comments are closed.