Support » Developing with WordPress » How do I set an orderby on a custom taxonomy template?

  • Resolved Rose

    (@eos-rose)


    I have a custom taxonomy template called taxonomy-series.php.

    The loop in this template is:

      <?php if (have_posts()) : ?>
        <div class="list-group mb-5">
          <?php while (have_posts()) : the_post(); ?>
            <?php get_template_part( 'includes/listgroup' ); ?>
          <?php endwhile; ?>
        </div>
      <?php endif; ?>

    I need to change the default loop so that the list of posts assigned to the taxonomy terms are ordered by the custom field called series_order_num.

    I tried this, but it completely ignored the taxonomy term and I don’t know how to include the term so that whatever term is assigned to the page is included:

    <?php $series_query = new WP_Query(array(
    	'post_type'				=> 'post',
    	'posts_per_page'	=> -1,
    	'meta_key'				=> 'series_part_num',
    	'orderby'					=> 'meta_value',
    	'order'						=> 'DESC'
    )); ?>
    <?php if( $series_query->have_posts() ): ?>
    	<div class="list-group mb-5">
    	<?php while( $$series_query->have_posts() ) : $series_query->the_post(); ?>
    		<?php get_template_part( 'includes/listgroup' ); ?>
    	<?php endwhile; ?>
    </div>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>

    I’m clearly missing something somewhere and need another pair of eyes.

    • This topic was modified 6 months, 2 weeks ago by  Rose.
Viewing 1 replies (of 1 total)
  • This worked for me:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
    <?php $args = array(
    	'post_status' => 'publish',
    	'post_type' 	=> 'post',
    	'tax_query' 	=> array(
    		array(
    			'taxonomy' 	=> 'series',
    			'field'    	=> 'slug',
    			'terms'    	=> $term->slug,
    		),
    	),
    	'meta_key'		=> 'series_part_num',
    	'orderby'			=> 'meta_value_num',
    	'order' 			=> 'ASC',
    );
    $query = new WP_Query( $args ); ?>
    
    <?php if( $query->have_posts() ): ?>
    	<div class="list-group mb-5">
    	<?php while( $query->have_posts() ) : $query->the_post(); ?>
    		<?php get_template_part( 'includes/listgroup' ); ?>
    	<?php endwhile; ?>
    </div>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
Viewing 1 replies (of 1 total)
  • The topic ‘How do I set an orderby on a custom taxonomy template?’ is closed to new replies.