Support

Account

Home Forums ACF PRO Sorting a list using repeater and multi-select field Reply To: Sorting a list using repeater and multi-select field

  • Just wanted to make sure, didn’t want to answer the wrong question. This may not be the best way to do this but it’s the way I’d do it because it is the most straight forward. What you need to do is put the values from the repeater into and array that can be sorted.

    
    $books = array();
    if (have_rows('books')) {
      while (have_rows('books')) {
        the_row();
        $category = get_sub_field('category');
        if (!isset($books[$category])) {
          $books[$category] = array();
        }
        $books[$category][] = get_sub_field('title');
      }
    }
    // sort the categories
    // ksort sorts the array by the element index
    ksort($books);
    // loop through books
    foreach ($books as $category => $list) {
      echo '<h2>',$category,'</h2'><ul>';
      // sort the list
      sort($list);
      foreach ($list as $book) {
        echo '<li>',$book,'</li>';
      }
      echo '</ul>';
    }