Home › Forums › Front-end Issues › Group a set post by date and arrange by time and repeat for each date
I’m new and I’m still new a coding php and I’m pretty sure there’s an answer for this but after looking for two weeks and searching Google I wanted to give this a try.
I’m using a custom field to group post by date. Only outputting the date once and group all of the posts under that date. Once all of the posts are found for the custom field date I want to return and loop back through to do the same thing for any additional dates. I’m creating a conference schedule separated by days with each activity under the day by time.
Text example (Also attached an image)
Col-1
First ACF Date Found ===
Post with same date
Post with same date
Post with same date
end of post with same date close col-1 block
Repeat found new date
Col-1
First ACF Date Found ===
Post with same date
Post with same date
Post with same date
end of post with same date close col-1 block
Repeat found new date
Col-1
First ACF Date Found ===
Post with same date
Post with same date
Post with same date
end of post with same date close col-1 block
No new date end query loop
<?php
$posts = get_posts(array(
'post_type' => 'schedule',
'posts_per_page' => -1,
'meta_key' => 'date_of_session',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<div class="col-lg col-md-6">
<div class="mad-shedule">
<header>
<h4><?php the_field('date_of_session'); ?></h4>
</header>
<!--================ Shedule Item ================-->
<div class="mad-shedule-item mad-dropdown">
<div class="mad-shedule-head">
<div class="mad-shedule-time">
<time datetime="2019-12-16"><?php the_field('time_session_start'); ?> - <?php the_field('time_session_end'); ?></time>
<?php if(get_field('short_summary_of_session')): ?>
<button><i class="licon-minus mad-dropdown-title"></i></button>
<?php endif; ?>
</div>
<h5 class="mad-shedule-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
<div class="mad-shadule-zone"><?php the_field( 'location_of_session' );?></div>
<?php if(get_field('short_summary_of_session')): ?>
<p class="mad-dropdown-element"><?php the_field('short_summary_of_session'); ?></p>
<?php endif; ?>
</div>
<?php if(get_field('whos_session_is_this')): ?>
<?php
/*
* Loop through post objects (assuming this is a multi-select field) ( setup postdata )
* Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop
* Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset
*/
$post_objects = get_field('whos_session_is_this');
if( $post_objects ): ?>
<?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<div class="mad-shedule-person">
<div class="mad-person-img">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
</div>
<div class="mad-person">
<h6 class="mad-person-name"><a href="#"><?php the_title(); ?></a></h6>
<span><?php the_field( 'title' );?> at <a href="<?php the_field( 'job_website' );?>"><?php the_field( 'job' );?></a></span>
</div>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif;?>
<?php endif; ?>
</div><!--================ End of Shedule Item ================-->
</div>
<!--================ End of Shedule ================-->
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.