• Hi all,

    Given this 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

    (@mcyzyk)

    Thanks!

    Is there any way to make this happen directly in my code above rather than in functions.php?

    Much appreciated,

    Mark

    Moderator bcworkz

    (@bcworkz)

    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.