Support

Account

Home Forums Add-ons Repeater Field Repeatable events ordered by date from repeater field Reply To: Repeatable events ordered by date from repeater field

  • It might also be possible to do this another way, but it would be just as complex and I cannot really provide specifics, all I can do is give you some ideas.

    1) Query all of the posts

    2) Loop over all of the posts and build an new array of posts that includes duplicates of each post for each date. You will need to add something to each element of your new array to sort by.

    3) Build a usort() function that orders the posts using the value you added to sort by.

    
    // query your posts
    $posts = array(); // hold your new list of posts
    // loop over posts
    while ($query->have_posts()) {
      $query->the_post();
      // loop over repeater
      while (have_rows('repeater_field')) {
        the_row();
        $post_to_sort = $post;
        $post_to_sort->start_date = get_sub_field('start_date', false);
        $posts[] = $post_to_sort;
      }
    }
    usort($posts, 'sort_posts_by_start_date');
    // loop over posts for display
    foreach ($posts as $post) {
      setup_postdata($post);
      // display post
    }
    wp_reset_postdata()
    

    AND

    
    // usort_function
    function sort_posts_by_start_date($a, $b) {
      if ($a->start_date == $b->start_date) {
        return 0;
      }
      if ($a->post_date < $b->post_date) {
        return -1;
      }
      return 1;
    }