Fake Archives for Multiple Authors

Generate a list of WordPress posts, grouped by month, for the specified author.

This comes in handy when working with multiple authors, and needing to generate a list of archives for their content.

<div id="archives_wrap">
	<h2>Archives</h2>
	<ul>
		<?php
			$author_id = get_the_author_meta('ID'); // get current author ID
			
			$blogtime = date('Y');
			$prev_limit_year = $blogtime - 1;
			$prev_month = '';
			$prev_year = '';
						
			$args = array(
				'posts_per_page' => 20,
				'ignore_sticky_posts' => 1,
				'author' => $author_id // return author id
			);
			
			$postsbymonth = new WP_Query($args);
			
			while($postsbymonth->have_posts()) {
						
			    $postsbymonth->the_post();
						
			    if(get_the_time('F') != $prev_month || get_the_time('Y') != $prev_year && get_the_time('Y') == $prev_limit_year) {
						
			        echo "<li>".get_the_time('F Y')."</li>nn"; // Date Format
						
			    } ?>

				<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p><!-- Post link and Title -->
						
			    <?php
						
			    $prev_month = get_the_time('F');
			    $prev_year = get_the_time('Y');
						
		} ?>

	</ul>
</div><!-- #archives_wrap -->

Example return:

  • August 2015
    • Post Title
    • Post Title
  • July 2015
    • Post Title
    • Post Title

Setup a toggle to hide the child items until clicked.

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.