WordPress.org

Support

Support » How-To and Troubleshooting » Sort posts based on a custom field

Sort posts based on a custom field

  • I am trying to sort posts based on a custom value and to do that I have tried replacing <?php while ( have_posts() ) : the_post(); ?> in my theme code to both <?php while ( query_posts('meta_key=326_dtg&orderby=meta_value') ) : the_post(); ?> and <?php while ( have_posts() ) : query_posts('meta_key=326_dtg&orderby=meta_value'); ?>) : the_post(); ?> but both of those simply get the same item to be displayed infinitely, in short it doesn’t works.

    So my question is, what am I doing wrong ( I have been around PHP a little but not equally around wordpress so not sure what I am doing here ) and using this technique will mean creating three separate index pages i.e. one for then newest items ( WP’s default ), another one for ascending order ( lowest items first ) and the third one for descending order ( highest items first ). I am sure this is not best practice, but still is it way out of the circle?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Howdy,

    Try:

    query_posts( 'meta_key=326_dtg&orderby=meta_value' );
    while( have_posts() ) : the_post();
    // do whatever you want to the loop of posts
    endwhile;

    You had the query_posts inside the while loop, so it was going to go on forever.

    Let me know if this is not clear.
    Paul

    Works like a charm!

    Thanks a lot!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sort posts based on a custom field’ is closed to new replies.