Reply To: Archive a news story

Home Forums GovIntranetters Archive a news story Reply To: Archive a news story

Matthew Jasek

Old post but someone may find this of use. If you want to display monthly archives of a custom post type (news) then you can use the following code.

Add this to your functions file

function my_custom_post_type_archive_where($where,$args){  
    $post_type  = isset($args['post_type'])  ? $args['post_type']  : 'post';  
    $where = "WHERE post_type = '$post_type' AND post_status = 'publish'";
    return $where;  

add_filter( 'getarchives_where','my_custom_post_type_archive_where',10,2);

Then where you want to call the archive list use this code (archive.php sidebar in my case):

<?php $args = array(
    'post_type'    => 'news',
    'type'         => 'monthly',
    'echo'         => 0
echo '<ul>'.wp_get_archives($args).'</ul>'; ?>

You can change the ‘type’ to ‘yearly’ if you want to list the posts by year rather than months.

If your outputted links are showing 404’s you will need to make some more modifications but this method works with the theme and permalinks set to ‘Post name’