Home › Forums › Front-end Issues › Sort by one field, and then by another › Reply To: Sort by one field, and then by another
Sooooo…y’all were super helpful before and I was able to apply this filtering to another website as well. All was going swimmingly until the client asked for one more level of sorting, and I’m tearing my hair out trying to figure this out.
So, this is for a choral group. They’re wanting to display all their past seasons, sorted by year/season, and then by performance date (performances are actually individual products in WooCommerce). Sorting by the ACF meta fields is going fine. But within each year, they also want to break it out to sorting all the concerts assigned the ‘cantata-series’ product category, and the ‘masterworks-series’ product category. So, a typical listing would look something like:
2017-2018 SEASON
Cantata Series
* Concert Date 1
* Concert Date 2
Masterworks Series
* Concert Date 1
* Concert Date 2
2016-2017 SEASON
Cantata Series
* Concert Date 1
* Concert Date 2
Masterworks Series
* Concert Date 1
* Concert Date 2
etc. etc. etc.
I was doing fine when it was just sorting by season and then by date. But adding in sorting by category is just throwing me. I can get it to display the correct series before each and every date it applies to, but that’s not right.
Here’s my code right now (not working)
$season_args = array(
'numberposts' => -1,
'post_type' => 'product',
'product_cat' => 'past, cantata-series, masterworks-series',
'meta_query' => array(
'season_clause' => array(
'key' => 'select_season',
'compare' => 'EXISTS',
),
'date_clause' => array(
'key' => 'date',
'compare' => 'EXISTS',
)
),
'orderby' => array(
'season_clause' => 'ASC',
'date_clause' => 'ASC'
)
);
$the_query = new WP_Query($season_args);
if ($the_query->have_posts()) {
// variables to hold values from previous post
$last_season = '';
$last_date = '';
while ($the_query->have_posts()) {
$the_query->the_post();
// get season and compare it to the previous post
$this_season = get_field('select_season');
if ($this_season != $last_season) {
// the season has changed
if ($last_season != '') {
// close the post UL, season DIV, and season DIV that we'll open next
// this will be skipped if this is the first post
// because $last_season will be empty
echo '</div><!-- .season -->';
} ?>
<div class="season">
<h2><?php echo $this_season->post_title; ?></h2>
<?php // clear $last_season
$last_season = '';
// set $last_season to $this_season
$last_season = $this_season; ?>
<?php if ( has_term( 'cantata-series', 'product_cat' ) ) { ?>
<h3>Cantata Series</h3>
<?php }
} // end if new season
// get the date and compare to previous post
$this_date = get_field('date', false, false);
$this_date = new DateTime($this_date); ?>
<?php if ( has_term( 'cantata-series', 'product_cat' ) ) { ?>
<li><?php echo $this_date->format('M j Y'); ?> - <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php } ?>
<?php } // end while have posts
} // end if have_posts
wp_reset_query();
Thanks in advance.
~Laura
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.