I would like to create an event calendar for the festival website to see some specific date performances sorted by date and time.
– Each band has its own post.
– Each band post has repeater field with unlimited rows (date/time/location).
Band repeater fields example (data will be entered by admins and not sorted at backend):
– 20160605 19:00 Somewhere1
– 20160606 17:00 Somewhere2
– 20160607 18:00 Somewhere3
– 20160606 15:00 Somewhere4
I have some events list page where f.e. 20160606 date is selected.
So on Calendar page i would like to have ability to use wp query and see all 20160606 date events as list, sorted by date and time from all posts.
Selected date: 20160606
I was trying some methods by using database query and so on, but nothing worked well for me. Any help with this?
You can use wp_query to call in specific posts using the meta_query parameter. There are some pretty shiny examples of this here. Check out number 4 for sub-fields.
What this would do is to give you all the posts that have band dates in on the set date. This will only do half of the job you need though, as it’s likely that a band will play twice on one day at different times.
Run the first query and within the loop I’d probably set an array to save the time as the key and the post ID as the value. This then allows you to use krsort(), which will arrange the array in descending order by the key. You can then do a loop through the array, and use the array value of that post’s ID to grab the band name / images / etc. from that particular post, and then output the key afterwards as the time they go on-stage.
Hopefully that gives you some things to go on, but if not let me know and post some code and we can see what’s going wrong and where.
Thank you for showing the way 🙂
I have tried that example code and now i have all events from each band on specific date. The problem is that i’m not sure how to sort it by time sub field.
Here is the code:
<?php $date = "20160620"; // args $args = array( 'numberposts' => -1, 'post_type' => 'band', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'events_%_date', 'compare' => '==', 'value' => $date, ) ) ); $the_query = new WP_Query( $args ); ?> <?php if( $the_query->have_posts() ): ?> <div class="events"> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php if(have_rows('events')): while(have_rows('events')): the_row(); if(get_sub_field('date') == $date): the_sub_field('time'); echo ' - '; the_title(); echo '<br>'; endif; endwhile; endif; ?> <?php endwhile; ?> </div> <?php endif; ?> <?php wp_reset_query(); ?>
By this code i’m getting such events lst:
1500 – Band1 (1st show)
1800 – Band 1 (2nd show)
1400 – Band 2 (2nd show)
1200 – Band 2 (1st show)
I’m not sure how to use krsort() with repeater. Can you help me with this?
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!
© 2023 Advanced Custom Fields.