wordpress snippets last updated   : April 22, 2014
we now have 631 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 →
  • 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

jqsnipp.com | we just launched jquery snippets website