wordpress snippets last updated   : September 29, 2014
we now have 639 snippets

Adding custom post types to wp_get_archives()

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will enable wp_get_archives to display your custom post types. Don’t forget to update your custom post type name on line 06.

snippet :  PHPcopy
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','CUSTOM_POST_TYPE_NAME' ) ); 
	$post_types = "'" . implode( "' , '" , $post_types ) . "'";
	return str_replace( "post_type = 'post'" , "post_type IN ( $post_types )" , $where ); 
} 
source →

Did you like this post? If so please share it with your friends. Thanks!

  • Dansedd30

    Thanks for that..however I’m having a bit of difficulty with the actual link, if I now click on say “February”, it takes me to the non custom post type archives in february. Instead of the CPT posts for Feb.  i.e. the link is just /2012/02/ 

    • Kvskinet

       yes, archive list links to post type “post” yearly archive instead of cpt yearly archive

  • Kvskinet

    404