Home › Forums › Add-ons › Repeater Field › Sort posts by date picker in repeater field
Hi!
I have a question and i hope you can help me with it.
A client of me would have a page with a index of his workshops.
Each workshop will be given on multiple dates
So i made a custom post type named workshops.
And make a repeater field named ‘dates’.
Inside of it i make a date_picker field named ‘date’.
Now i want to make a index sorted on date, so in the index is it possible that a workshop placed more then once but with another date.
How can i make this index, sorted by the date field? I don’t know how and hope you can help me…
PS. sorry for my bad English
i think you need to loop through your CP and Repeater and build a array first, and after that echo workshops 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();
$my_workshop_name = get_field('my_workshop_name');
if( have_rows('dates') ):
while ( have_rows('dates') ) : the_row();
$my_workshop_date = get_sub_field('date');
$the_ID = get_the_ID();
/*of course you can extend this with additional fields or infos for that workshop that you can use later*/
$array[$my_workshop_date][$the_ID]['date'] = $my_workshop_date;
$array[$my_workshop_date][$the_ID]['name'] = $my_workshop_name;
endwhile;
endif;
endwhile;
endif;
/*output*/
asort($array);
foreach ($my_workshop_date as $key_day => $row_day){
foreach ($row_day as $key_workshop => $row_id){
$workshop_name = $row_id['name'];
$workshop_date = $row_id['date'];
$workshop_date_pretty = date_i18n( 'j. F Y', $workshop_date);
echo $workshop_date_pretty .' : '. $workshop_name;
}
}
hope that help
Hi,
Thanks for your answer. Something i don’t understand or it was not good. BUT, with this code it works fine for me:
<?php
/*build and fill: cp-loop and repeater loop*/
if(have_posts()) : while(have_posts()) : the_post();
$my_workshop_name = get_the_title();
if( have_rows('datums') ):
while ( have_rows('datums') ) : the_row();
$my_workshop_date = get_sub_field('datum');
$the_ID = get_the_ID();
/*of course you can extend this with additional fields or infos for that workshop that you can use later*/
$workshops[$my_workshop_date][$the_ID]['date'] = $my_workshop_date;
$workshops[$my_workshop_date][$the_ID]['name'] = $my_workshop_name;
endwhile;
endif;
endwhile;
endif;
/*output*/
// [20151201]=> array(1) { [29]=> array(2) { ["date"]=> string(8) "20151201" ["name"]=> string(18) "TEST123 4-12-2015" } }
//$datum = date("Ymd");
ksort($workshops);
foreach ($workshops as $key_day => $row_day){
if ($key_day > date("Ymd")) {
foreach ($row_day as $key_workshop => $row_workshop){
$workshop_name = $row_workshop['name'];
$workshop_date = $row_workshop['date'];
echo $workshop_date .' : '. $workshop_name .'<br />';
}
}
}
?>
The topic ‘Sort posts by date picker in repeater field’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.