Support

Account

Home Forums Front-end Issues Group posts by custom field value

Solved

Group posts by custom field value

  • Hello, I’m using ACF with the FacetWP plugin to list out event locations on a page. The states in which these events are located are stored as a text custom field. I’m trying to organize the locations underneath their corresponding state, and list them all out on one page. This is what the page currently looks like: http://d2slive.staging.wpengine.com/locations/. As you can see, it’s just listing out one state’s results, but it’s doing it multiple times. It should look more like this page: https://dare2sharelive.org/locations/ (that page looks correct, but core functionality of sorting via the dropdown is broken).

    Here’s the code I’m using:

    <?php
    
    while ($query->have_posts()) {
        $query->the_post();
        $post_id = get_the_ID();
        $title = get_the_title($post_id);
        $distance = facetwp_get_distance($post_id);
        $distance = (false !== $distance) ? round($distance, 1) . ' miles away' : '';
        $coords = get_post_meta($post_id, 'location', true);
        $content = get_the_content();
    
        $state = get_field('state');
        $state_posts[$state][] = $post;
    }
    
    foreach ($state_posts as $state_post => $state_title) {
      ?>
      <h1 class="state-name"><?php echo esc_html($state); ?></h1>
      <?php
      foreach ($state_title as $listing) {
        ?>
        <div class="post-item" data-title="<?php echo esc_attr($title); ?>"
          data-latitude="<?php echo $coords['lat']; ?>" data-longitude="<?php echo $coords['lng']; ?>" data-distance="<?php echo $distance; ?>">
          <div class="post-item-content">
            <h2><?php echo $title; ?></h2>
            <?php echo $content; ?>
          </div>
        </div>
        <?php
      }
    }
    ?>

    What could I do differently?

  • Found the solution:

    <?php
    
        $state_posts = array();
    
        while ($query->have_posts()) {
            $query->the_post();
            $state = get_post_meta(get_the_ID(), 'state', true);
            $state_posts[$state][] = $post;
        }
    
        wp_reset_query();
    
        foreach ($state_posts as $state_post => $state_title) {
    ?>
      <h1 class="state-name"><?php echo esc_html($state_post); ?></h1>
    <?php
      foreach ($state_title as $listing => $single_listing) {
        setup_postdata($single_listing);
        $post_id = $single_listing->ID;
        $title = get_the_title($post_id);
        $distance = facetwp_get_distance($post_id);
        $distance = (false !== $distance) ? round($distance, 1) . ' miles away' : '';
        $coords = get_post_meta($post_id, 'location', true);
        $content = get_the_content();
    ?>
      <div class="post-item" data-title="<?php echo esc_attr($title); ?>" data-latitude="<?php echo $coords['lat']; ?>" data-longitude="<?php echo $coords['lng']; ?>" data-distance="<?php echo $distance; ?>">
        <div class="post-item-content">
          <h2><?php echo $title; ?></h2>
          <?php echo $content; ?>
        </div>
      </div>
    <?php
      }
      wp_reset_postdata();
    }
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Group posts by custom field value’ is closed to new replies.