Function to display all posts by month of publication
I hope I haven't bored you with my functions yet.
In this post, I will tell you about another function for displaying in WordPress. However, this time the function is not mine, but from the Blix Theme theme author. The function has evolved from the theme into a plugin, by the efforts of the rmarsh.com blog author, who extracted it from the theme and created a plugin called "Blix Archive".
I have no idea how Dimox found this plugin, Dimox is a gold seeker "Why mention Dimox?" - you may ask. "Why not?" - I'll reply. It was he who, a long time ago, gave me a link to this plugin after I, being a green WordPress digger, turned to the "web development guru" Dimox to show me the code of his sitemap. By the way, if you're not familiar with Dima and his excellent blog, get acquainted immediately.
After Dimox kindly directed me to the link where I downloaded the plugin, I put everything back in its place - buried the plugin after extracting the display function, which I will now share with you.
So that's the story behind the function of this post
Now to the point.
The function I'm going to introduce displays posts, grouping them by the month of publication, and it looks like this:

And the function itself looks like this:
/** * Function to display posts by month. * * @param int $show_comment_count Display comment count. 1 - true. * @param string $before ('<h4>') HTML tag before heading (month name). * @param string $after ('</h4>') HTML tag after heading. * @param string $year (0) Limit output by year. If set to 2009, posts for 2009 will be displayed by months. * @param string $post_type ('post') Post type. Use if a custom post type needs to be displayed (different from post) * @param int $limit (100) Limit the number of posts displayed for each month. * Creates significant load with a large database. Set to 0 to remove limit. * * Usage example: * <php echo get_blix_archive(1, '<h4>', '</h4>'); ?> */ function get_blix_archive( $show_comment_count = 0, $before = '<h4>', $after = '</h4>', $year = 0, $post_type = 'post', $limit = 100 ) { global $month, $wpdb; $result = ''; $AND_year = $year ? $wpdb->prepare( " AND YEAR(post_date) = %s", $year ) : ''; $LIMIT = $limit ? $wpdb->prepare( " LIMIT %d", $limit ) : ''; $post_type = esc_sql( $post_type ); $arcresults = $wpdb->get_results( " SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM $wpdb->posts WHERE post_type='$post_type' $AND_year AND post_status='publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC " ); if( ! $arcresults ){ return $result; } foreach( $arcresults as $arcresult ){ $url = get_month_link( $arcresult->year, $arcresult->month ); $text = sprintf( '%s %d', $month[ zeroise( $arcresult->month, 2 ) ], $arcresult->year ); $result .= get_archives_link( $url, $text, '', $before, $after ); $thismonth = zeroise( $arcresult->month, 2 ); $thisyear = $arcresult->year; $arcresults2 = $wpdb->get_results( " SELECT ID, post_date, post_title, comment_status, guid, comment_count FROM $wpdb->posts WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_status='publish' AND post_type='$post_type' AND post_password='' ORDER BY post_date DESC $LIMIT " ); if( $arcresults2 ){ $result .= "<ul class=\"postspermonth\">\n"; foreach( $arcresults2 as $arcresult2 ){ if( $arcresult2->post_date != '0000-00-00 00:00:00' ){ $url = get_permalink( $arcresult2->ID ); //$arcresult2->guid; $arc_title = $arcresult2->post_title; if( $arc_title ){ $text = strip_tags( $arc_title ); } else{ $text = $arcresult2->ID; } $result .= "<li>" . get_archives_link( $url, $text, '' ); if( $show_comment_count ){ $cc = $arcresult2->comment_count; if( $arcresult2->comment_status == "open" or $comments_count > 0 ){ $result .= " ($cc)"; } } $result .= "</li>\n"; } } $result .= "</ul>\n"; } } return $result; }
This code should be inserted into the theme's functions.php file or directly into the file where this function is used (that's usually what I do), and where we want to display the list, we insert this code:
<?php echo get_blix_archive(1, '<h4>', '</h4>'); ?>
Use CSS selectors .postspermonth{...} and .postspermonth li{...} to customize the appearance of the lists.
Advanced usage
During the function's evolution, it underwent a number of changes and now, by my efforts, it can:
Limit output by year
<?php echo get_blix_archive(1, '<h4>', '</h4>', 2009); ?>
Only posts for the year 2009 will be displayed.
Understand which post type to display
Posts can be displayed by month, specifying the post type we want to display. This is necessary if a new post type, different from post, is registered on the site. Suppose the new post type is called post_type, then the code will be as follows:
<?php echo get_blix_archive( 1, '<h4>', '</h4>', 0, 'post_type' ); ?>
Limit the output
The final parameter passed to the function is the limiter (limit) for the number of posts displayed for each month. I added this because unlimited output placed significant load on the processor when testing this function on a site with 20,000 posts. By default, a maximum of 100 entries will be displayed for each month.