Group recent WordPress posts by date published

On a news site, it’s often helpful to group posts in reverse chronological order, with a date heading for all articles that were published on that date. You can achieve that with this snippet:

<?php $my_query = new WP_Query( array( 'showposts' => 20, 'post_type' => 'news' ) );
while( $my_query->have_posts() ): $my_query->the_post();
	$i = get_the_date( 'F j, Y' );
	if ($i == $j):
		// do nothing
	else :
		if ( $k == 1 ): ?>
			</ul>
		<?php endif; 
		$j = $i;
		$k = 1; ?>
		<h4><?php echo $i; ?></h4>
		<ul>
	<?php endif; ?>
	<li>
		<span class="news-title">
			<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
		</span>
	</li>
<?php endwhile; ?>
</ul>

https://gist.github.com/theukedge/a0f5c4e9a572820d103e

Image: http://min.us/i/2sWDseCCYTWr

About

Dave Clements enjoys building and maintaining WordPress site, eating hummus, seeing justice served, drinking English cider, watching the BBC and spending time with his amazing wife and beautiful daughter.

5 thoughts on “Group recent WordPress posts by date published

  1. Yesterday’s snippet used deprecated code from before WP 2.0 and today it is bingo again! This time the deprecated function dates back from WP 2.1.

    showposts should obviously be replaced with posts_per_page.

    Can you please pay attention before posting your handy code snippets?

    • You are right again Piet but, deprecated doesn’t mean it will not work. I’m grateful for your astute attention to detail but I’m also very grateful to those who have taken the time to submit snippets, some of whom are very prominent in the community. Managing WP Snippets is just a little contribution from myself and is meant to serve as a little festive fun, it’s not my full time job as I do have a day job. If you would like to make a contribution yourself or indeed help manage WP Snippets as an Editor next year I’d be equally grateful to you too.

      • Sorry you feel that way Paul, I guess it’s the result of 2 in a row. Not aware I have been unfriendly, must be the difference between native and non-native English.

        Perhaps a good idea to read things in context before blurting out things to strangers. Merry Christmas to you too, Sir

  2. Hi Elliott, I know that deprecated doesn’t mean it doesn’t work, but as we’re moving towards 2014 and version 4.x I think it is best to leave the old behind :)

    I realise that some of your contributors are prominent in the community, and to be honest that is why I was quite shocked to see a deprecated function pop up…

    Anyways, I have an entire repository filled with Code Snippets, you are more than welcome to pull one from there. Or if you want me to choose one for you, I’d be happy to do so too.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>