Group loop by category

I asked this question on the WordPress Stack Exchange, and it did not receive most of the answer after 24 hours. So I thought I would bring him to a larger community.

In any case, I am creating an event plugin that works, however I have a slight problem nailing the list page. It shows all the events, but I want them to group them by month. The statement should display the date of the event, capture the month and group entries together that fall out in that particular month. I know this will be foreach, but I'm not sure how to write it.

Here is my loop:

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

// Build It
if ($the_query->have_posts()) :
    ?>
    <div id="event-list">
        <?php
        global $post;

        $month_array = array();
        while ($the_query->have_posts()) : $the_query->the_post();

            var_dump( get_post_meta( $post->ID, '_eDate', true ) ); 

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

            if ($the_query != 0 && $month_number != $month_array[$the_query->current_post - 1]) //LINE 38
                echo '<h2>' . $month_format->format( 'F' ) . '</h2>'; 
            ?>

            <div class="row">
                <div class="event-image">
                    <a href="<?php echo get_permalink(get_the_ID()); ?>">
                        <?php
                        if (has_post_thumbnail()) {
                            the_post_thumbnail('thumbnail');
                        }
                        ?>
                    </a>
                </div>
                <div class="event-content">
                    <h3><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a></h3>
                    <div class="event-date"><?php display_event_date(); ?></div>
                    <div class="event-time"><?php display_event_time(); ?></div>
                    <div class="event-price"><?php display_event_price(); ?></div>
                    <br/>
                    <a href="<?php echo get_permalink(get_the_ID()); ?>" class="event-info">More Information</a>
                </div>
                <div class="event-buy">
                    <?php display_event_buy_online_url(); ?>
                </div>
            </div>
        </div>
        <?php wp_reset_postdata(); ?>
        <?php
    endwhile;
endif;

--- EDIT 1 ---

I updated my block of code based on input.

var_dump outputs in this way string(10) "2015-07-09"

I also get two errors.

. WP_Query int C:\xampp\apps\wordpress\htdocs\wp-content\plugins\wp-events-em4b\views\shortcode.php 38

: Undefined offset: -1 C:\xampp\apps\wordpress\htdocs\wp-content\plugins\wp-events-em4b\views\shortcode.php 38

, int , , , , var_dump .

+4
2

WPSE, . ,

Y-m-d, . , .

, , order orderby.

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

, usort() . , , .

, Y-m-d, _eDate

() , , . foreach,

, , . var_dump( get_post_meta( $post->ID, '_eDate', true ) );, , . . _eDate, . ,

 // Query Post Type
$args = array(
    'post_type' => 'events',
    'post_status' => 'publish',
    'meta_key' => '_eDate',
    'orderby' => 'meta_value' //HAD A BUG HERE, DID NOT SORT CORRECTLY, NEEDS TO BE meta_value
);
$the_query = new WP_Query($args);

// Build It
if ( $the_query->have_posts() ) :
    ?>
    <div id="event-list">
        <?php
        $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 ); // HAD A BUG HERE, CONVERTED WRONG DATE
            $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 //HAD A BUG HERE, WAS $the_query != 0
                 && $month_number != $month_array[$the_query->current_post - 1] 
            )
                echo '<h2>' . $month_format->format( 'F' ) . '</h2>'; // Output month name as January            
            ?>

            <div class="row">
                <div class="event-image">
                    <a href="<?php echo get_permalink(get_the_ID()); ?>">
                        <?php
                        if (has_post_thumbnail()) {
                            the_post_thumbnail('thumbnail');
                        }
                        ?>
                    </a>
                </div>
                <div class="event-content">
                    <h3><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a></h3>
                    <div class="event-date"><?php display_event_date(); ?></div>
                    <div class="event-time"><?php display_event_time(); ?></div>
                    <div class="event-price"><?php display_event_price(); ?></div>
                    <br/>
                    <a href="<?php echo get_permalink(get_the_ID()); ?>" class="event-info">More Information</a>
                </div>
                <div class="event-buy">
                    <?php display_event_buy_online_url(); ?>
                </div>
            </div>
        </div>
        <?php wp_reset_postdata(); ?>
        <?php
    endwhile;
endif;

. , . , .

+2

. post , , , , .

, , , .

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

$the_query = new WP_Query($args);
$posts = array(); // array for storing posts by month

// Build It
if ($the_query->have_posts()) :
    ?>
    ob_start(); // start buffering the output from the 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>
                    <div class="event-date"><?php display_event_date(); ?></div>
                    <div class="event-time"><?php display_event_time(); ?></div>
                    <div class="event-price"><?php display_event_price(); ?></div>
                    <br/>
                    <a href="<?php echo get_permalink(get_the_ID()); ?>">More Information</a>
                </div>
                <div class="event-buy alignleft">

                    <div class="event-buy">
                        <a class="buy-btn btn" href="<?php display_event_buy_online_url(); ?>" target="_blank">Buy Online</a>
                    </div>
                </div>
            </div>
            <hr>
        </div>
        $post = ob_get_contents(); // get the contents of the post to a variable
        ob_end_clean(); // clear output buffer
        $month = date('m', strtotime(get_event_date())); // get event month *THIS NEEDS TO BE CHANGED*
        $posts[$month][] = $post; // append post to month array
        <?php wp_reset_postdata(); ?>
        <?php
    endwhile;

    sort($posts); // sort post array by month numbers

    foreach($posts as $month) {
        // starting to display posts for a new month
        foreach($month as $post) {
            echo $post; // output the saved post from variable
        }
    }
endif;

:

$args = (
    'orderby' => 'event_date',
    'order' =>   'ASC',`
);

, html, .

0
source

All Articles