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 have a custom post type "Kalender_item" with a custom Date Field (YYMMDD). I want to list all the posts on a page sorted by Year and Month.

For example:

  • November 2012 (all events that occure in November 2012)
  • December 2012 (all events that occure in December 2012)

And so on...

I have succeeded in ordering them like so


This gives me all my posts in the correct order. Now I want to group them by Month Year and display the Month Year as a title for each group. How to group my results by year and month?

share|improve this question
Hi and welcome, I'm not sure if I interpreted well your original question. Please confirm, and if not, edit it again :) – brasofilo Nov 27 '12 at 1:57
@brasofilo you interpreted it correctly. Any chance you can help me out? – Mister Melotte Nov 27 '12 at 21:11
Sorry, I don't have a similar setup to test this scenario. Take a look at this search query. Confirm the Custom Fields arguments for the query. Maybe format the query_posts like this. Or, more important, use WP_Query instead. Have you checked ACF forums? – brasofilo Nov 27 '12 at 22:44
up vote 3 down vote accepted

This should get you started:


$the_query = new WP_Query( array(
    'post_type'   => 'kalender_item',
    'post_status' => 'publish',
    'meta_key'    => 'kalender_item_datum',
    'orderby'     => 'meta_value'
) );

# This will hold what group we're in
$current_header = '';

# The Loop
while ( $the_query->have_posts() ) :

    # get the datum for this post
    $temp_date = get_post_meta( get_the_ID(), 'kalender_item_datum', true );

    # If they aren't the same, we'll start a new group, which for now
    # just means setting a new heading
    if ( $temp_date != $current_header ) {
        $current_header = $temp_date;
        echo "<h2>$current_header</h2>";

    # ... do normal loop stuff here


share|improve this answer
Thanks a lot, this was the help I was looking for. Now it looks so simple. That's what you get when designers want to develop. – Mister Melotte Dec 7 '12 at 14:28

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.