Support

Account

Home Forums Front-end Issues Sorting taxonomies not working

Solved

Sorting taxonomies not working

  • Hello,

    I’m thrilled to use ACF now, I don’t know why I wasn’t using it before!

    Small issue here on this web site: https://www.leshivernales.be/les-danseurs/

    I have created a custom post type called “Danseurs” with simple content. I added a “Année” taxonomy (year) so I could sort out the dancers based on the years when they attended the event.

    The taxonomy terms are in the correct order (2012-2023) and in the taxonomy settings I’ve used the option “Sort the terms”

    But when you go on the output, they are grouped by taxonomy (which is perfect) but the taxonomy terms are absolutely not sorted 🙁

    What am I doing wrong?
    Even worse, it seems to be random and not always the same thing when you refresh the page …

    Below is the code used to display the dancers grouped by years (taxonomy)
    Any help would really be appreciated!

    Thanks,
    Antoine

    <?php 
    					$annees = get_terms( array(
    						'taxonomy' => 'annee',
    						'hide_empty' => true
    						)
    					);
    
    					$order = array();
    					foreach( $annees as $i => $annee ) {
    						$order[ $i ] = $annee->count;
    					}
    					
    					array_multisort( $order, SORT_DESC, $annees );
    					
    					//endforeach;
    					
    					foreach( $annees as $annee ) : 
    					
    					$args = array(
    						'post_type'         => 'danseur',
    						'posts_per_page'    => -1,
    						'orderby' => 'title',
    						'order' => 'ASC',
    						'tax_query' => array(
    							array(
    								'taxonomy' => 'annee',
    								'terms' => $annee->slug,
    								'field' => 'slug',
    								'orderby' => 'menu_order',
    								'order' => 'ASC',
    							),
    						),							    
    					);
    					$danseurs = new WP_Query($args); 
    				?>
    					
    				<div class="row danseur-list mb-5">
    					<h2 class="years h3 col-12"><?php echo $annee->name; ?></h2>
    				
    					<?php foreach( $danseurs->posts as $post ) : setup_postdata( $post ); ?>
    					
    					<div class="box col-6 col-lg-3 col-xl-2 mb-3">
    						<div class="inner">
    							<?php echo the_post_thumbnail( 'large', array('class' => 'img-fluid')); ?>
    							<div class="p-3">
    								<a>" title="<?php the_title(); ?>" class="stretched-link"><?php the_title(); ?></a>
    							</div>
    						</div>
    					</div>
    					
    					<?php endforeach; wp_reset_postdata(); ?>
    				</div>
    					
    				<?php endforeach; ?>
  • Hello again,

    Worked all night on it and did a lot of searching. It seems it was due to a post-reordering plugin that conflicts with tax_query()

    So glad I found the issue 😉

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

You must be logged in to reply to this topic.