I am trying to only show a single post per month on a query.

I can set the date as a variable and use something like the php array_unique function to check the dates and only echo out unique ones, however this feels a bit 'hacky'. Is there a built in query variable, or something built in WordPress I can use instead?

As for the 'hacky' option, I am quite new with PHP and struggling with using the aforementioned function. Checking the date is unique and only echoing out said variables when it is already inside a while loop is confusing me somewhat.

Below is the query I am using currently.

    $news_archive = array(      
        'post_type'=> 'news',
        'posts_per_page' => 6,
        'orderby' => 'date'


        echo '<ul class="related_links">';   

    while ( have_posts() ) : the_post();  

        $date = the_date('F m Y','','',false);
        $date = explode(' ', $date);    

        $month_str = $date[0];
        $month_int = $date[1];
        $year = $date[2];            

        echo '<li><a href="/archive/news/' . $year . '/' . $month_int . '/">';
        echo $month_str . ' ' . $year;          
        echo '</a></li>';     


        echo '</ul>';   




1 Answer 1


Maybe a bit less hacky:

$months = array();


  list($month, $year) = explode('/', get_the_date('n/y')); // eg. 7/11
  if(in_array($month, $months)) continue; // skip

  $months[] = $month;

  echo '<li><a href="'.get_month_link($month, $year).'">'.get_the_date('F Y').'</a></li>';


If you're only getting 6 posts and you don't need pagination that I guess it's OK to go this way.

Otherwise you need to build your own SQL query to pull out only one post for each month...

You could also try to make 6 queries for each month, like:

$january_posts = new WP_Query('monthnum=1&posts_per_page=1'));
  • Thanks, this works perfectly. Just trying to get my head around it!
    – danixd
    Jul 19, 2011 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.