1

I have done a bunch of searching and can't seem to find a resolution particular to what I'm looking to do. I've also tried to post this on the ACF support forums, but it kept deleting my post after editing it....at any rate, here's what's up:

I created an event custom post type. Within each of those posts, are repeaters which allow the person to select multiple date and times. What I would like to do, and I'm not sure it's possible the way I'm thinking, is group the events that have the same dates, by date.

Something like this:

Date 1

  • Event 1
  • Event 2
  • Event 3

Date 2

  • Event 2
  • Event 3

Date 3

  • Event 1
  • Event 2

And so fourth and so on....

While I've come across similar issues with potential resolutions, I've got nothing to work. Here's how I'm handling my code:

Outside the while loop:

$current_header = '';

Inside the while loop:

$date_count = count(get_field('dates_times'));

// Group events
for ($i = 0; $i < $date_count; $i++) : 

    // Get repeater fields 
    $dates = 'dates_times_' . $i . '_date';

    // Snatch up the postmeta
    $temp_date = get_post_meta( get_the_ID(), $dates, true );

    // Convert Date
    $date = DateTime::createFromFormat('Ymd', $temp_date);
    if ( is_object($date) ) $day = $date->format('l - F, j');

    // Group by day
    if ( $day !=  $current_header ) :
        $current_header = $day;
        echo '<h2 class="medium-12 cell">' . $day . '</h2>';
    endif; 

    // REST OF POST CODE HERE

endfor;

This returns:

Date 1

  • Event 1

Date 3

  • Event 1

Date 1

  • Event 2

Date 2

  • Event 2

Date 3

  • Event 2

Date 1

  • Event 3

And so on.... They're not being grouped as expected. I assume the for loop is throwing them off. So any ideas or suggestions here would be greatly appreciated.

Thanks.

2
  • Can Event_1 have several dates that differ by time? In the // REST OF POST CODE HERE you get all events with date $date and display their names, right?
    – nmr
    Jul 26, 2018 at 19:07
  • They can. I originally had the other options (time & location) showing above, but since they won't affect how the posts are grouped, I decided to hide them. Jul 26, 2018 at 19:14

1 Answer 1

0

Start by collecting all the dates of the current post (event) in the array. Then iterate throught the array and get all events for each date.

// all dates from current event in format "Ymd"
$all_dates = [];
for ($i = 0; $i < $date_count; $i++) {

    // Get repeater fields 
    $dates = 'dates_times_' . $i . '_date';
    $temp_date = get_post_meta( get_the_ID(), $dates, true );

    // if value of dates_times_XX_date is in format 'Ymd' conversion is not needed

    // Group by day
    if ( !isset( $all_dates[$temp_date] ) )
        $all_dates[] = $temp_date;
}
if ( !empty($all_dates) )
    sort($all_dates);

foreach ($all_dates as $l_date) {

    $query = "select post_id from {$wpdb->postmeta} where meta_key like 'dates_times_%_date' and meta_value=%s";

    // IDs of events with that date
    $events = $wpdb->query( $wpdb->prepare($query, $l_date) );

    // get events with IDs, 
    // display header '<h2 class="medium-12 cell"> date </h2>';
    // display events 
    // REST OF POST CODE HERE
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.