Home › Forums › ACF PRO › Sort by Repeater-Date field › Reply To: Sort by Repeater-Date field
i think you need to loop through your CP and Repeater and build a array first, and after that echo events out of that array.
important for ordering by date is: that you set Return Format to Ymd (you can change this later with date/date_i18n)
/*build and fill: cp-loop and repeater loop*/
if(have_posts()) : while(have_posts()) : the_post();
$get_event_name = get_field('my_event_name');
if( have_rows('repeater_field_name') ):
while ( have_rows('repeater_field_name') ) : the_row();
$my_event_date = get_sub_field('my_event_date');
$get_date = (get_sub_field('my_event_date'));
$get_date_day = date('d', strtotime($get_date));
$get_date_month = date('m', strtotime($get_date));
$get_date_year = date('Y', strtotime($get_date));
$the_ID = get_the_ID();
/*of course you can extend this with additional fields or infos for that event that you can use later*/
$array[$get_date_year][$get_date_month][$get_date_day][$the_ID]['date'] = $my_event_date;
$array[$get_date_year][$get_date_month][$get_date_day][$the_ID]['name'] = $my_event_name;
endwhile;
endif;
endwhile;
endif;
/*output*/
/*i assume you extract year and month from today (example here next month)*/
$today = date('Ymd');
$d = new DateTime($today);
$d->modify( 'first day of next month' );
$year_youwish = $d->format( 'Y' );
$month_youwish = $d->format( 'm' );
$array_month_youwish = $array[$year_youwish][$month_youwish];
asort($array_month_youwish);
foreach ($array_month_youwish as $key_day => $row_day){
foreach ($row_day as $key_event => $row_id){
$event_name = $row_id['name'];
$event_date = $row_id['date'];
$event_date_pretty = date_i18n( 'j. F Y', $event_date);
echo $event_date_pretty .' : '. $event_name;
}
}
hope that help
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 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.