Get monthly archives for custom post type

I know this is a rather common topic, but I can’t seem to resolve the issue I’m having. Any help is greatly appreciated, I’m spinning my wheels at this point.

Using Custom Post Type UI plugin, I’ve created some custom post types (I’ll use ‘blog’ in this example). I’ve set has_archive => true, and I’ve also used a plugin called Custom Post Type Archives to get my pagination to work properly. So far, I have my permalinks, tags, categories and pagination working. However, the issue is when I try to use the wp_get_archives function. I understand this doesn’t grab CPTs, so I’ve used the function that comes with the Custom Post Type Archives Plugin (wp_get_custom_post_archives). Depending on how I tweak the arguments, it spits out either a 404, or it loads the archives, but just displays every entry (not just those specific to the selected month). Ideally, I just want it to display monthly archives.

<?php wp_get_post_type_archives('blog');

Take a look here:
http://www.metropoliscreative.com/coding/w/blog/

I’ve used this to register the post type and believe I’ve done it properly. Not sure what I’m doing wrong at this point.

register_post_type('blog', array(   
        'label' => 'Blog',
        'description' => 'Blog Entries',
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array(
            'slug' => 'blog'),
        'has_archive' => true,
    'query_var' => true,
    'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes',),
    'taxonomies' => array('category','post_tag',),
    'labels' => array (
      'name' => 'Blog',
      'singular_name' => 'Blog',
      'menu_name' => 'Blog',
      'add_new' => 'Add Blog',
      'add_new_item' => 'Add New Blog',
      'edit' => 'Edit',
      'edit_item' => 'Edit Blog',
      'new_item' => 'New Blog',
      'view' => 'View Blog',
      'view_item' => 'View Blog',
      'search_items' => 'Search Blog',
      'not_found' => 'No Blog Found',
      'not_found_in_trash' => 'No Blog Found in Trash',
      'parent' => 'Parent Blog',
    ),) );

1 Answer
1

You can use getarchives_where hook of wp_get_archives() function

Add this function to your functions.php:

function Cpt_getarchives_where_filter( $where , $r ) {

  $post_type="blog";
  return str_replace( "post_type="post"" , "post_type="$post_type"" , $where );
}

Then when you want your monthly archive put this:

add_filter( 'getarchives_where' , 'Cpt_getarchives_where_filter' , 10 , 2 );
wp_get_archives();
remove_filter('getarchives_where' , 'Cpt_getarchives_where_filter' , 10 );

Leave a Comment