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 responses to “Adding Custom Post Types To wp_get_archives()