Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function posts_by_year() {
- // array to use for results
- $years = array();
- // get posts from WP
- $posts = get_posts(array(
- 'numberposts' => -1,
- 'orderby' => 'post_date',
- 'order' => 'ASC',
- 'post_type' => 'my-custom-post-type',
- 'post_status' => 'publish'
- ));
- // loop through posts, populating $years arrays
- foreach($posts as $post) {
- $years[date('Y', strtotime($post->post_date))][] = $post;
- }
- // reverse sort by year
- krsort($years);
- return $years;
- }
- <?php foreach(posts_by_year() as $year => $posts) : ?>
- <h2><?php echo $year; ?></h2>
- // the code that I need to display the post counts per year
- <?php endforeach; ?>
Advertisement
Add Comment
Please, Sign In to add comment