I am having some trouble figuring out the logic of this query and what I need to do to make it work.

I created an "Event" custom post type, and every Event post has a date value coming from a custom field.

I want to display on my page Event posts from the current date until 6 months back, organized month by month (image at the end of this post)

I am kind of lost with that task.

That's where I arrived so far, I can retrieve the posts backward from my current day on, but I am still not sure how to display them month by month (under a button for every month).

    // Query

    $the_query = new WP_Query(array(
        'post_type' => 'event',
        'posts_per_page' => -1,
        'meta_key' => 'date_picker',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'meta_query' => array(
                'key' => 'date_picker',
                'value' => date('Ymd'),
                'compare' => '<=',
                'type' => 'DATE'


    <?php if ($the_query->have_posts()) : ?>
    <?php while ($the_query->have_posts()) : $the_query->the_post();

            // vars for latest
            $event_image = get_field('event_image');
            $date_picker = get_field('date_picker');
            $time_picker = get_field('time_picker');
            $event_location = get_field('event_location');
            $label_opt = get_field_object('label_options');


    <div class="event-col">
        <div class="event-thumb-image">

            <img src="<?php echo $event_image ?>" alt="event">

            <!-- add labels -->
            <?php if (!empty($label_opt)) : ?>

            <?php $labels = $label_opt['value']; ?>

            <div class="event-labels">
                <?php foreach ($labels as $label) : ?>

                <?php if ($label == '1') : ?>
                <span class="label">Profis</span>
                <? elseif ($label == '2') : ?>
                <span class="label">Workshop</span>
                <? endif; ?>

                <?php endforeach; ?>
            <?php endif; ?>


        <div class="event-thumb-text">
                <h2><?php the_title(); ?></h2>

                <span class="latest-events-date">
                            // change date format
                            $format_in = 'D, d.m.y';
                            $format_out = 'M';

                            $date = DateTime::createFromFormat($format_in, $date_picker);

                            echo $date->format($format_out);
                <span class="latest-events-time-place"><?php echo $time_picker ?> -
                    <?php echo $event_location ?></span>
    <?php endwhile; ?>
    <?php endif; ?>

    <?php wp_reset_query(); ?>

That's the page I am developing, no success yet.

enter image description here

Thanks a lot <3

  • Just loop through the posts and if the month of the current post is different to the previous post output a heading for that month. Dec 10, 2022 at 2:39


Your Answer

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