WordPress: How to sort posts on a custom field

I want to display a list of post titles from a custom taxonomy in a particular order.
I thought the best way to control the order would be to add a custom field and sort on that custom field.

The problem I’m having is that I’m trying to use the built-in functionality of WordPress and I can’t find a way to add sort functionality.

Read More

My Scenario looks like this
The calling url is …com/taxonomy/term

This calls up a template, the filename of which is taxonomy-taxonomyname-term.php

My template is simply the index.php template renamed and edited to contain this loop

<?php if ( have_posts() ) : ?>

<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( array( 'before' => 'Permalink to: ', 'after' => '' ) ); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>

<?php else : ?> etc

This displays a list of titles but I can’t find a way to control the order of the titles.

The only way I’ve seen to set the order of a group of posts is to define the order of the posts in the query. But of course in this case I dont have a query because I already have the posts via the calling url.

Is there any way to add sort functionality without adding another query or is the query mandatory.

Related posts

Leave a Reply

1 comment

  1. Suppose your custom fields is my_date
    You can create custom query like this.

    query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
    

    To use it

    <?php if ( have_posts() ) : ?>
    
    <?php twentyeleven_content_nav( 'nav-above' ); ?>
    <ul>
    <?php /* Start the Loop */ 
      query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
    ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( array( 'before' => 'Permalink to: ', 'after' => '' ) ); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
    </ul>
    <?php twentyeleven_content_nav( 'nav-below' ); ?>
    
    <?php else : ?> etc
    

    For more info http://wpengineer.com/1915/sort-posts-custom-fields/