I had some problem with wp_get_archives
, you can try my solution of this issue
function get_cpt_archives( $cpt, $echo = false ) {
global $wpdb;
$sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status="publish" GROUP BY YEAR(wp_posts.post_date), MONTH(wp_posts.post_date) ORDER BY wp_posts.post_date DESC", $cpt);
$results = $wpdb->get_results($sql);
if ( $results ){
$archive = array();
foreach ($results as $r){
$year = date('Y', strtotime( $r->post_date ) );
$month = date('F', strtotime( $r->post_date ) );
$month_num = date('m', strtotime( $r->post_date ) );
$link = get_bloginfo('siteurl') . "https://wordpress.stackexchange.com/" . $cpt . "https://wordpress.stackexchange.com/" . $year . "https://wordpress.stackexchange.com/" . $month_num;
$this_archive = array( 'month' => $month, 'year' => $year, 'link' => $link );
array_push( $archive, $this_archive );
}
if( !$echo )
return $archive;
foreach( $archive as $a ){
echo '<li><a href="' . $a['link'] . '">' . $a['month'] . ' ' . $a['year'] . '</a></li>';
}
}
return false;
}
put this in functions.php of your theme.
Then call this function where you need:
<?php get_cpt_archives('post_type', true); ?>
'post_type'
– change this to your post type name.
Second parameter:
true
– it’s a list with <li>
false
– it’s an array.