WordPress.org

Ready to get started?Download WordPress

Forums

Sort posts based on a custom field (3 posts)

  1. orgzchaos
    Member
    Posted 2 years ago #

    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?

  2. Paul Kaiser
    Member
    Posted 2 years ago #

    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

  3. orgzchaos
    Member
    Posted 2 years ago #

    Works like a charm!

    Thanks a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.