WP Query + custom fields: How to query event posts from the current date backwards 6 months and organize it month by month?

Question

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).

<?php
    // 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(
            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; ?>
            </div>
            <?php endif; ?>

        </div>

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

            <div>
                <span class="latest-events-date">
                    <?php
                            // 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>
                <span class="latest-events-time-place"><?php echo $time_picker ?> -
                    <?php echo $event_location ?></span>
            </div>
        </div>
    </div>
    <?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

0
Ramate 2 months 2022-12-09T19:42:06-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse