WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying Custom Taxonomy Archive in Ascending Order (3 posts)

  1. the.green.timtam
    Member
    Posted 1 year ago #

    Hello,

    I'm having issues displaying my taxonomy archive loops in ascending order.

    I have a taxonomy called 'enemies' which applies to four custom post types. I want to display all of them in a loop in ascending order.

    So far I've got this in my taxonomy-enemies.php file:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
            <?php endwhile; ?>
          <?php endif; ?>

    which displays 10 in descending order. I tried adding query_posts:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
    <?php query_posts(array('posts_per_page' => -1, 'orderby'=> date, 'order'=>ASC));
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    But that makes the loop only display posts (not custom post types), even though they are not given the enemies taxonomy. I tried this:

    <?php query_posts(array('posts_per_page' => -1, 'post_type' => array('post-type-1', 'post-type-2', 'post-type-3', 'post-type-4'), 'orderby'=> date, 'order'=>ASC, 'taxonomy'=>'enemies', 'term'=>'enemy-slug'));
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    This works perfectly when I replace 'enemy-slug' with the slug of an enemy, but I can't work out how to make it generate from the archive that is currently being displayed.

  2. the.green.timtam
    Member
    Posted 1 year ago #

    I got it!

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
    <?php query_posts(array('posts_per_page' => -1, 'post_type' => array('doctor-who-story', 'sja-story', 'torchwood-story', 'k9-story'), 'orderby'=> date, 'order'=>ASC, 'taxonomy'=>'enemies', 'term'=>$term->slug));
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>
    <?php endif; ?>
  3. andrew.croce
    Member
    Posted 8 months ago #

    While what you have will work, it is not the idea way. Essentially you are creating a second query on top of the main query. In many cases this will not make a noticeable difference, but for more complex queries, and in the interest of doing things correctly, you should modify the main query before it reaches the template.

    In your functions.php:

    function my_query_modifications( $query ) {
    
      // Are we querying a taxonomy archive?
      if( $query->is_main_query() && $query->is_tax()) {
    
        // If so, modify the order variables
        $query->set( 'orderby', 'date' );
        $query->set( 'order', 'ASC' );
    
      }
    }
    add_action('pre_get_posts', 'my_query_modifications');

    We are hooking into the action prior to running the query, so we can modify it as needed. First we check if its the main query (and not some sub-query we might not want to modify). Then we check if its a taxonomy we're querying. Then we simply set the query variables we need to change.

    Note this will work to change any query variable, not just order.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.