Support

Account

Home Forums ACF PRO Sorting repeater fields by date Reply To: Sorting repeater fields by date

  • What is the name of the repeater field and what is the name of the field you want to sort on?

    
    // get the entire repeater in a multidimensional array
    $repeater = get_field('YOUR-REPEATER');
    usort($repeater, 'SORT_REPEATER_BY_YOUR_SUB_FIELD');
    
    // usort function
    function SORT_REPEATER_BY_YOUR_SUB_FIELD($a, $b) {
      if ($a['YOUR-SUB-FIELD'] == $b['YOUR-SUB-FIELD']) {
        return 0;
      } elseif ($a['YOUR-SUB-FIELD'] < $b['YOUR-SUB-FIELD']) {
        return -1;
      } else {
        return 1;
      }
    }
    

    Once you’ve done the above then you loop over the array, in order to do this sorting you cannot use the acf have_rows() loop

    
    foreach ($repeater as $row) {
      // show items from this row
      // example, to echo one of the sub fields
      echo $row['SUB-FILED-NAME'];
    }