Home › Forums › Add-ons › Repeater Field › Repeatable events ordered by date from repeater field › Reply To: Repeatable events ordered by date from repeater field
It might also be possible to do this another way, but it would be just as complex and I cannot really provide specifics, all I can do is give you some ideas.
1) Query all of the posts
2) Loop over all of the posts and build an new array of posts that includes duplicates of each post for each date. You will need to add something to each element of your new array to sort by.
3) Build a usort() function that orders the posts using the value you added to sort by.
// query your posts
$posts = array(); // hold your new list of posts
// loop over posts
while ($query->have_posts()) {
$query->the_post();
// loop over repeater
while (have_rows('repeater_field')) {
the_row();
$post_to_sort = $post;
$post_to_sort->start_date = get_sub_field('start_date', false);
$posts[] = $post_to_sort;
}
}
usort($posts, 'sort_posts_by_start_date');
// loop over posts for display
foreach ($posts as $post) {
setup_postdata($post);
// display post
}
wp_reset_postdata()
AND
// usort_function
function sort_posts_by_start_date($a, $b) {
if ($a->start_date == $b->start_date) {
return 0;
}
if ($a->post_date < $b->post_date) {
return -1;
}
return 1;
}
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 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.