Support

Account

Home Forums Front-end Issues Sort by one field, and then by another Reply To: Sort by one field, and then by another

  • Well, I have another site I’m trying to use this sorting feature on, but for some reason it’s just not working this time, and I’m not sure why. Hoping someone here can help me figure it out.

    Everything displays, except that the <h2> gets added before each item (meaning, for instance, that I get:

    ALABAMA

    • Property Name

    ARKANSAS

    • Property Name

    FLORIDA

    • Property Name

    FLORIDA

    • Property Name

    And it needs to show those last two as

    FLORIDA

    • Property Name
    • Property Name

    Here’s my code:

    $props_args = array(
       'post_type' => 'properties',
       'posts_per_page' => -1,
       'post_status' => 'publish',
       'meta_query' => array(
          'state_clause' => array(
             'key' => 'which_state',
             'compare' => 'EXISTS',
          ),
       ),
       'orderby' => array(
          'state_clause' => 'ASC',
          'title' => 'ASC'
       ),
       'order' => 'DESC',
    );
    
    $props_query = new WP_Query($props_args);
    if ($props_query->have_posts()) { ?>
    
    <main class="container">
       <div class="row">
    
          <?php // variables to hold values from previous post
          $last_state = '';
    
          while ($props_query->have_posts()) {
             $props_query->the_post();
    
             // get state and compare it to the previous post
             $this_state = get_field('which_state');
    
             if ($this_state != $last_state) {
    
                // the state has changed
                if ($last_state != '') {
                   // close the post UL, city DIV, and state DIV that we'll open next
                   // this will be skipped if this is the first post
                   // because $last_state will be empty
                   echo '</tbody></table><!-- list --></div><!-- state -->';
                }
                echo '<div class="state"><h2>'.$this_state.'</h2>';
             } // end if new state
    
             // Vars
             $marker = get_field('address');
             $address = explode( "," , $marker['address']);
             $logo = get_field('property_logo'); ?>
    
             <tr>
                <td class="gold-border"></td>
                <td class="first">
                   <a class="link" href="<?php the_permalink(); ?>" target="_blank"><img src="<?php echo $logo; ?>" class="style-svg" alt="<?php the_title(); ?>"></a>
                </td>
                <td>
                   <?php echo $address[0].''; //street number ?><br />
                   <?php echo $address[1].','.$address[2]; //city, state + zip ?>
                </td>
             </tr>
          <?php  } // end while have posts
    
          // we need to close the last set of elements that were opened
          echo '</tbody></table><!-- list --></div><!-- state -->'; ?>
    
       </div>
    </main>
    
    <?php } // end if have_posts
    
    wp_reset_query();