WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>';   



share|improve this question
up vote 1 down vote accepted

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'));
share|improve this answer
Thanks, this works perfectly. Just trying to get my head around it! – danixd Jul 19 '11 at 14:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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