0

I have wp query for posts. I'm ordering them by custom field "event_date". (The field format example is 09.08.2016). I'm extracting year and month from each post and adding it as div class, however i want to wrap all posts by parent div for each month. At frontend i want it to have like this:

<div id="082016">
  <div class="post 082016"><?php the_title();?></div>
  <div class="post 082016"><?php the_title();?></div>
  <div class="post 082016"><?php the_title();?></div>
</div>
<div id="092016">
  <div class="post 092016"><?php the_title();?></div>
  <div class="post 092016"><?php the_title();?></div>
</div>
<div id="102016">
  <div class="post 102016"><?php the_title();?></div>
  <div class="post 102016"><?php the_title();?></div>
  <div class="post 102016"><?php the_title();?></div>
</div>

And so on. Is it possible to wrap all posts to such parent div? How should i do it?

<?php 
$args = array(
  'posts_per_page' => -1,
  'post_type' => 'event',
  'meta_key' => 'event_date',
  'orderby' => 'meta_value_num',
  'order' => 'ASC'
);

$wpquery = new WP_Query($args); ?>
<?php while( $wpquery->have_posts() ): $wpquery->the_post();  ?>
    <?php
    $date = get_field('event_date'); 
    $year = date('Y',strtotime($date)); 
    $month = date('m',strtotime($date)); 
    $day = date('d',strtotime($date));
    ?>
    <div class="<?php echo $month; echo $year;?>">
        <h1><?php the_title();?></h1>
    </div>
    <?php endwhile; ?> 
<?php wp_reset_postdata(); ?>

Many thanks for any help!

1 Answer 1

0

This cannot be done only by querying database. Firstly, you have to iterate through all items and create array of posts grouped by dates.

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'event',
    'meta_key' => 'event_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

$wpquery = new WP_Query($args);
$posts = $wpquery->get_posts();
$ordered_posts = array();

foreach ($posts as $post) {
    $meta_value = get_post_meta($post->ID, 'event_date', true);
    if (!$meta_value) {
        continue;
    }
    $date = date('mY', strtotime($meta_value));
    $ordered_posts[$date][] = $post;
}

Next, you have to iterate through grouped array to display divs as you want:

foreach ($ordered_posts as $post_date => $posts) { ?>
    <div id="<?php echo $post_date; ?>">
        <?php foreach ($posts as $post): ?>
            <div class="post <?php echo $post_date; ?>">
                <?php echo $post->post_title; ?>
            </div>
        <?php endforeach; ?>
    </div>
<? }
2
  • Great! It works just perfectly :) The only thing i have changed $date = date('mY', strtotime(get_field('event_date'))); to sort by custom field, not published date.
    – MIC
    Aug 9, 2016 at 10:47
  • Yeah, didn't noticed that you want to sort it by custom field. Happy to see that you handle this by yourself :) EDIT. Now answer is fixed Aug 9, 2016 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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