Home Forums ACF PRO Sort by Repeater-Date field


Sort by Repeater-Date field

  • Hello,

    I have a custom post type “events” with a repeater that contains a date. So I have mutliple events and each event has multiple dates.

    Now I want to display all events of a specific month sorted by the date.

    Any clue how achive this.

    thanks werner

  • No ideas? Any tip would be awesome.

  • 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;
    /*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];
    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

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Sort by Repeater-Date field’ is closed to new replies.