Title: Adding sort order to main query
Last modified: November 28, 2023

---

# Adding sort order to main query

 *  [mcyzyk](https://wordpress.org/support/users/mcyzyk/)
 * (@mcyzyk)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/adding-sort-order-to-main-query/)
 * Hi all,
 * Given this code:
 *     ```wp-block-code
       <?php if(have_posts()) :  ?>	
       <?php while(have_posts()) :  ?>	
       <?php the_post(); ?>		
       <?php
           $last_name = get_post_meta( get_the_ID(), 'last_name', true );
           $first_name = get_post_meta( get_the_ID(), 'first_name', true );
           $short_title = get_post_meta( get_the_ID(), 'short_title', true );
           $date = get_post_meta( get_the_ID(), 'date', true );
       ?>
       <div class="site-width">
   
           <div class="tax-desc">
             <div class="theMainEntry">
               <?php echo $last_name ?>, <?php echo $first_name ?>. <a href="<?php the_permalink(); ?>"><?php echo $short_title ?>...</a> <?php echo $date ?><p>
             </div>  
            </div>
   
   
       </div><!-- post -->
   
       <?php endwhile;  ?>
       ```
   
 * How can I sort (orderby) by $short_title, then by $date?
 * Thanks,
 * Mark

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

 *  Thread Starter [mcyzyk](https://wordpress.org/support/users/mcyzyk/)
 * (@mcyzyk)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/adding-sort-order-to-main-query/#post-17236890)
 * Thanks!
 * Is there any way to make this happen directly in my code above rather than in
   functions.php?
 * Much appreciated,
 * Mark
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/adding-sort-order-to-main-query/#post-17236905)
 * I take it your code occurs on a template? You would have to discard the main 
   query and instantiate a new WP_Query object with appropriate arguments. “pre_get_posts”
   action and possibly other action hooks is the only practical way to modify main
   queries. The code does not necessarily need to be in functions.php, it could 
   be in another .php file that’s included or required, or code could go in a plugin’s.
   php file. But it has to execute well before your template loads.

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

The topic ‘Adding sort order to main query’ is closed to new replies.

## Tags

 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [sort order](https://wordpress.org/support/topic-tag/sort-order/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [2 years, 6 months ago](https://wordpress.org/support/topic/adding-sort-order-to-main-query/#post-17236905)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
