0

I have a site that lists upcoming events and past events.

In my category-past-event.php template, this is how I've gotten past events appear in descending order (from most recent to oldest):

    <?php
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $args = array(
                  'posts_per_page' => 100,
                  'paged' => $paged,
                  'cat' => 3709,
                  'orderby' => 'meta_value',
                  'meta_key' => 'Deadline', //formatted YYYYMMDD
                  'order' => 'desc'
                );

                query_posts($args);     
        ?>

This setup successfully lists events in the order they appeared, e.g.

  • Event 6
  • Event 5
  • Event 4
  • Event 3
  • Event 2
  • Event 1

How can I get it so that the past events are broken up by month? E.g.

December 2012
- Event 6
- Event 5
- Event 4

November 2012
- Event 3
- Event 2
- Event 1

Update: I have set up pre_get_posts in functions.php as @s_ha_dum suggests and this is working.

@KrzysiekDróżdż, the problem I have is that the Deadline might not correspond to get_the_time('F Y').

This is because I might create a post with the Deadline = 20131212 (i.e. taking place in December 2013), but the time of that post might be different. I.e. it is June, and I create an upcoming event for December, the time of that post is June because I created it in June.

Currently using this on a production site (though I am testing locally with MAMP). The workflow for creating events is that posts are categorized as Events, and then the Post Expirator plugin switches their category to Past-Event once the date of the event has passed. Deadline exists so the posts can be sorted in ascending or descending order.

Still trying to wrap my head around how in the loop in category-past-event.php I would use that Deadline to sort them by month

2 Answers 2

0

Using query_posts is pretty much always a bad idea as it causes another database query but it also will break pagination and can cause other issues as well.

What you need it a filter on pre_get_posts.

function pgp($qry) {
  if (!is_admin() && is_main_query() && $qry->is_category(3709)) {
    $qry->set('orderby','meta_value');
    $qry->set('meta_key','Deadline'); //formatted YYYYMMDD
    $qry->set('ignore_sticky_posts',true);
  }
  return $qry;
}
add_filter('pre_get_posts','pgp');

That is assuming that category ID 3709 is the "past-event" category. Once you've done that follow the answer by @KrzysiekDrozdz

0

First of all I don't think you should use your custom query_posts in here. WordPress already queries posts so it's waste of time to query them one more time. You should be using pre_get_posts filter or somthing like that.

What can you do to split posts by month? You have to do this manually in your loop. Somthing like this should solve your problem:

$prev_month = '';

while ( have_posts() ):
    the_post();
    $deadline = strtotime(get_post_meta($post->ID, 'Deadline', true)); // this line may need to be changed - it depends on the format you choosed to store dates in deadline meta value
    $current_month = date('F Y', $deadline);  
    if ( $current_month != $prev_month ) {
        echo '<h2>'. $current_month .'</h2>';
        $prev_month = $current_month;

    // output your post/event
endwhile;

It's not tested so it can be buggy, but the idea behind this solution should be clear.

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.