Support

Account

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

Solving

Repeatable events ordered by date from repeater field

  • Hi, I have cpt ‘events’ and trying to do repeatable events.
    Simplyfy – an event has ‘title’ and ‘start_date’. Dates are in repeater field (‘start_date_repeater’) becouse one event can repeat.
    Here is my code:

    <?php
    $today = current_time(‘Y-m-d’);
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘events’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘start_date_repeater’,
    ‘compare’ => ‘>=’,
    ‘value’ => $today,
    ),
    ),
    ‘meta_key’ => ‘start_date_repeater’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘ASC’
    ));
    ?>

    <?php if( have_rows(‘start_date_repeater’) ):
    while( have_rows(‘start_date_repeater’) ) : the_row();
    <div>
    <h3><?php echo the_field(‘title’); ?></h3>
    <div><?php echo the_sub_field(‘start_date’); ?></div>
    </div>
    <?php endwhile; endif; ?>

    I need output like that:
    event01, 15.03.2023
    event02, 16.03.2023
    event01, 17.03.2023
    event01, 18.03.2023
    event02, 19.03.2023

    But I got that:
    event01, 15.03.2023
    event01, 17.03.2023
    event01, 18.03.2023
    event02, 16.03.2023
    event02, 19.03.2023

    How can I order events by date not by title?

  • first thing is that I do not see a have_posts() loop in your code, but this will not matter.

    It is impossible using WP_Query() to list a post more than once. It is also impossible to order posts by a repeater sub field. So you’ve basically set an impossible task for yourself using only these things and it requires something much more complex.

    In order to show a post multiple times, well, basically you need multiple posts. If you look at the way others have done this you will find that they also have a second post type that is hidden from users. When an event is saved what happens is that a new post in the hidden post type is created for each of the dates entered when creating the event and each of these posts is related to the event post (parent). Then the archive page for events actually uses this post type to get all of the events listed in the correct order and pulls most of the data from the “parent” event.

    There are likely other ways to do this, but the method I’ve described is used by the most popular events plugin.

  • 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;
    }
    
  • Thx a lot but this does not work for me.

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

You must be logged in to reply to this topic.