Trying to output posts on an archive page with date titles. Ideally this can be a single query so I can paginate the archive. Currently I can call multiple queries for each date item but I don't think this will allow for pagination.

First the structure:

This Week (Posts here)

Last Week (Posts here)

June (Posts here)

May (Posts here)

April (Posts here)


For instance if I want to paginate every 20 posts and I have 15 posts from this week and 10 in the last week, I would want to start page 2 with the posts from last week.

My current code can loop through the months, but not paginate:


    // echo 'current: '.date('F Y').'<br />';

    $start = $month = strtotime(date('F Y'));
    $end = strtotime('2010-01-01');

    echo $start . '<br>';
    echo $end;

    while($month > $end)
         echo '<h3>'.date('F Y', $month), PHP_EOL.'</h3>';

                    $box_post_args = array(
                        'post_type'         => 'post',
                        'paged'             => $paged,
                        'posts_per_page'    => -1,
                        'category__in' => $vet_category,
                        'date_query'     => array(
                                'year'  => date('m', $month), PHP_EOL,
                                'month' => date('Y', $month), PHP_EOL


         $month = strtotime("-1 month", $month);


1 Answer 1


I changed my approach in the end. I just called a single query and added the logic to check the date of the last post was in a different month:

            $today = date("r");
            $articledate = get_the_time('r');
            $difference = round((strtotime($today) - strtotime($articledate))/(24*60*60),0);

                if ($month_sort == true && ($difference <= 7) && $current_week == null) {
                    $vpost_count = 0;
                    echo '</h3><div class="clear"></div><h3>This Week</h3><div class="clear"></div>';
                    $current_week = 'set'; 

                elseif ($month_sort == true && $current_month != get_the_date('F') && $difference >= 7) {
                    $vpost_count = 0;
                    echo '</h3><div class="clear"></div><h3>'.get_the_date('F').'</h3><div class="clear"></div>'; 
                    $current_month = get_the_date('F');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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