Home / functions.php / Adding custom post types to wp_get_archives()
  • How to Adding custom post types to wp_get_archives()

    adding-custom-post-types-to-wp_get_archives

    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.

    wordpress snippet : PHP

    <>
    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 ); 
    } 
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_post, get_post_type, get_post_types, add_filter, _n,
  • 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