Support

Account

Home Forums Add-ons Repeater Field List posts grouped by sub-field of repeater

Solving

List posts grouped by sub-field of repeater

  • Hi,
    I wonder id anyone can help me on this issue:
    I have a custom post type (events) organised under 2 categories (past and current), each event has several occurrences managed with a repeater field (evento-paragens) with several sub-fields: event-date, event-hour, event-venue (this last one is a relational field).

    What I pretend is to list the events by occurrence and grouped by date:

    DAY 1
    HOUR — EVENT A — CITY
    HOUR — EVENT B — CITY
    HOUR — EVENT A — CITY
    HOUR — EVENT B — CITY

    DAY 2
    HOUR — EVENT A — CITY
    HOUR — EVENT B — CITY
    HOUR — EVENT A — CITY
    HOUR — EVENT B — CITY

    Until now what I could achieve was a list of all occurrences grouped by event:

    DAY 1 — HOUR — EVENT A — CITY
    DAY 1 — HOUR — EVENT A — CITY
    DAY 2 — HOUR — EVENT A — CITY
    DAY 2 — HOUR — EVENT A — CITY

    DAY 1 — HOUR — EVENT B — CITY
    DAY 1 — HOUR — EVENT B — CITY
    DAY 2 — HOUR — EVENT B — CITY
    DAY 2 — HOUR — EVENT B — CITY

    I’ve searched for a lot of forums but it seems that none of the examples suites my case. Maybe because the I need to group by a sub-field.

    Any ideas?
    M

  • @macreixa what does your WP_Query look like?

  • for now I have this:

    <?php
    $args = array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘eventos’,
    ‘status’ => ‘programa’,
    ‘orderby’ => array(
    ‘title’ => ‘ASC’)
    );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <hr>
    <?php the_field(‘evento-viagem’); ?> / <?php the_field(‘evento-audiencia’); ?><br>
    “><?php the_title(); ?><br>
    <?php if( get_field(‘evento-subtitulo’) ): ?>
    <?php the_field(‘evento-subtitulo’); ?><br>
    <?php endif; ?>
    <?php the_field(‘evento-caminho’); ?> <?php the_field(‘evento-ano’); ?><br>

    <?php if( have_rows(‘evento-paragens’) ): ?>

      <?php while( have_rows(‘evento-paragens’) ): the_row(); ?>

    • <?php the_sub_field(‘evento-data’); ?> — <?php the_sub_field(‘evento-hora’); ?>

      <?php
      $posts = get_sub_field(‘evento-local’);
      if( $posts ): ?>
      <?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
      <?php echo get_the_title( $p->ID ); ?> <?php the_field(‘local-cidade’, $p->ID); ?>
      <?php endforeach; ?>
      <?php endif; ?>

    • <?php endwhile; ?>

    <?php endif; ?>

    <?php endwhile; ?>

    wich Outputs something like this:

    DAY 1 — HOUR — EVENT A — CITY
    DAY 1 — HOUR — EVENT A — CITY
    DAY 2 — HOUR — EVENT A — CITY
    DAY 2 — HOUR — EVENT A — CITY

    DAY 1 — HOUR — EVENT B — CITY
    DAY 1 — HOUR — EVENT B — CITY
    DAY 2 — HOUR — EVENT B — CITY
    DAY 2 — HOUR — EVENT B — CITY

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

The topic ‘List posts grouped by sub-field of repeater’ is closed to new replies.