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](http://codex.wordpress.org/Template_Tags/wp_get_archives)
[^2]: [Custom post type permalinks](http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#comment-1765)

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

  1. Also having this issue.. the wp_get_archives function (set to yearly type) brings up domain.com/2011 which includes all post types… I need to be able to have it so if the archive is accessed via the custom post type section, it only brings up those, and not everything…

    any thoughts/help would be greatly appreciated!! been looking everywhere.

  2. This is just what I was after, thanks!

    I am using this in a select drop down with wp_get_archives – can I specify which custom post type to use? As I have several but only want to show one type.

Comments are closed.