Show posts by quarter

How would I list an author’s WordPress posts by quarter in author.php?

Current relevant author.php code is as follows (post is custom type “press mentions”, FYI).

This presents a single list of all this author’s posts.
But how do I break this list up in to segments, with quarterly headers, ie. “2015 Q3”, “2015 Q4″, 2016 Q1”, “2016 Q2”?

Quarters are four three-month periods running from January 1.

Posts should appear in same order as present, but segmented by quarter. So, the latest quarterly heading appears at top and so forth.

    <!-- Press appearances -->
      // Count press appearances
      $post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'pressmention' AND post_status = 'publish'");
    <h5><?php echo '<span class="label label-primary">'.$post_count.'</span>'; ?> Media appearances:</h5>
    <table class="table table-striped table-sm">
      <?php // Show custom post list -
          $args = array(
          'post_type' => 'pressmention' ,
          'author' => get_queried_object_id(), // this will be the author ID on the author page
          'showposts' => 200
      $custom_posts = new WP_Query( $args );

      if ( $custom_posts->have_posts() ): while ( $custom_posts->have_posts() ) : $custom_posts->the_post();?>
        <!-- changed th, removed scope="row", was width="110" and "125" -->
        <td class="col-xs-2"><?php the_time('M d, Y'); ?></td>
        <td class="col-xs-3"><img src="<?php echo get_post_meta( $post->ID,'press_mention_information_publication-url',true); ?>" alt="Publication"> <?php echo get_post_meta( $post->ID,'press_mention_information_publication-name',true); ?></td>
        <td class="col-xs-7"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></td>
      <?php endwhile; else: ?>
          <!-- changed th, removed scope="row" -->
          <tr><td colspan="2">No posts.</td></tr>
      <?php endif; ?>

Solutions Collecting From Web of "Show posts by quarter"

Let’s define a helper function to map a month to a quarter:

function wpse_get_month2quarter( $month )
    return ceil( $month / 3 );

We could then consider what to do with input values that are not integers in the set {1, ..., 12}.

ps: I got the idea for the formula from this Excel blog by Gašper Kamenšek

To get the Quarter within the loop we can e.g. use:

echo wpse_get_month2quarter( get_the_date( 'n' ) );


Here’s one idea for your loop:

if ( $custom_posts->have_posts() ) : 

    $prev = null;

    while ( $custom_posts->have_posts() ) : 


        $current = sprintf( 
            '%d Q%d', 
            get_the_date( 'Y' ), 
            wpse_get_month2quarter( get_the_date( 'n' ) )

        if( $prev !== $current )
            printf( '<tr><td colspan="3">%s</td></tr>', $current );
            $prev = $current;

        // ... etc ...

where we inject each quarter section once and assume that query is ordered by date.

But there are many ways to implement this.

If possible you should try to use and modify the main query, via pre_get_posts, instead of introducing another secondary query.