1

I am building an event plugin using a Custom Post Type. I have a loop running that will display all events on a page using a shortcode. My question is how can I set it up so that it groups the events by the month set in the metabox _eDate and places that group under one header. For example, all events in July will be displayed under a July h1 tag.

Here is the loop that I am currently using:

function events_list_shortcode() {

    // Query Post Type
    $args = array(
        'post_type' => 'events',
        'post_status' => 'publish'
);

$i = 0;
$the_query = new WP_Query($args);

// Build It
if ($the_query->have_posts()) :
    ?>
    <div id="event-list">
        <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
            <div class="row">
                <div class="event-image alignleft">
                    <a href="<?php echo get_permalink(get_the_ID()); ?>">
                        <?php
                        if (has_post_thumbnail()) {
                            the_post_thumbnail('thumbnail');
                        }
                        ?>
                    </a>
                </div>
                <div class="event-content alignleft">
                    <h3><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a></h3>
                    <?php
                    $eventDate = strtotime(get_post_meta(get_the_ID(), '_eDate', true));

                    if (!empty($eventDate)) {
                        ?>
                        <div class="event-date">
                            <?php echo date('F - j', $eventDate); ?>
                        </div>
                    <?php } ?>

                    <?php
                    $eventTime = strtotime(get_post_meta(get_the_ID(), '_eTime', true));

                    if (!empty($eventDate)) {
                        ?>
                        <div class="event-time">
                            <?php echo date('g:i A', $eventTime); ?>
                        </div>
                    <?php } ?>

                    <?php
                    $eventPrice = get_post_meta(get_the_ID(), '_ePrice', true);

                    if (!empty($eventPrice)) {
                        ?>
                        <div class="event-price">
                            <?php echo $eventPrice; ?>
                        </div>
                    <?php } ?>

                    <br/>
                    <a href="<?php echo get_permalink(get_the_ID()); ?>">More Information</a>
                </div>
                <div class="event-buy alignleft">
                    <?php
                    $eventBuy = get_post_meta(get_the_ID(), '_eBuy', true);

                    if (!empty($eventDate)) {
                        ?>
                        <div class="event-buy">
                            <a class="buy-btn btn" href="<?php echo $eventBuy ?>" target="_blank">Buy Online</a>
                        </div>
                    <?php } ?> 
                </div>
            </div>
        </div>
        <?php wp_reset_postdata(); ?>
        <?php
    endwhile;
endif;
}
1

If your dates are saved in the following format, Y-m-d, it is quite easy. Your year will stay the same, so your can then technically sort by month.

All you need is to add the correct values to the order and orderby parameters. You can try the following

// Query Post Type
$args = array(
    'post_type' => 'events',
    'post_status' => 'publish',
    'meta_key' => '_eDate',
    'orderby' => 'meta_value'
);

EDIT

Since you have your sorting done by the date in _eDate, displaying the month number by a set of posts in the same month is easy. You just need to compare the previous post's _eDate with the current post's _eDare, and if they don't match, output the month name.

EXAMPLE:

 // Query Post Type
$args = array(
    'post_type' => 'events',
    'post_status' => 'publish',
    'meta_key' => '_eDate',
    'orderby' => 'meta_value'
);
$the_query = new WP_Query($args);

// Build It
if ( $the_query->have_posts() ) {
    $month_array = array();
    while ( $the_query->have_posts() ) {
    $the_query->the_post();

    $date_field = get_post_meta( $post->ID, '_eDate', true );
    $month_format = DateTime::createFromFormat( 'Y-m-d', $date_field );
    $month_number = $month_format->format('m');
    $month_array[] = $month_number; 

    // Choose the format you want to display as indicated below
    if ( $the_query->current_post == 0 ) // Output date id this is the first post
        echo '<h2>' . $month_format->format( 'F' ) . '</h2>'; // Output month name as January

    if (    $the_query->current_post != 0
         && $month_number != $month_array[$the_query->current_post - 1] 
    )
        echo '<h2>' . $month_format->format( 'F' ) . '</h2>'; // Output month name as January            


        // DISPLAY YOUR POSTS AS PER NORMAL

    } // endwhile
    wp_reset_postdata();
} // endif
7
  • No dice. I applied your suggestion and it didn't sort. Would this still group by month, or would it simply order it by month? For example, i want all events that are in July to appear under a h1 tag saying July. – Geoffrey Jun 22 '15 at 19:04
  • This info should have been in your question. My solution orders posts by custom field date, only if the date in the custom field is in the correct format. Please be specific on what your question is next time. I'm off to bed now unfortunately – Pieter Goosen Jun 22 '15 at 19:11
  • Thank you for your help either way, i updated my question to be more specific. – Geoffrey Jun 22 '15 at 19:12
  • @Geoffrey - what is your date format? if you order the query by date and events aren't grouped naturally as a result, then your date format is probably wrong. outputting the header for each group is simply a process of checking the month of each event and outputting it when the current event's month differs from the previous. – Milo Jun 22 '15 at 19:27
  • @Milo the date is being submitted 2015-07-01 but is being stored in the table as 'longtext', i'm not sure if that makes a difference. – Geoffrey Jun 22 '15 at 19:30

Your Answer

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

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