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
ARKANSAS
FLORIDA
FLORIDA
And it needs to show those last two as
FLORIDA
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();
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.