WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i use below code in home page for show a table of custom fields. but i don't know how sort this table by duration value.

<?php while(have_posts()): the_post() ; ?>
<?php if (get_post_meta($post->ID, 'duration', true)>'1') :  { ?>
<tr>    <?php global $wp_query; $postid = $wp_query->post->ID; ?>
    <td>    <?php echo get_post_meta($postid, 'target', true); ?> </td>
    <td>    <?php echo get_post_meta($postid, 'price', true); ?> </td>
    <td>    <?php echo get_post_meta($postid, 'duration', true); ?> </td>
    <?php wp_reset_query(); ?> </tr>
<?php } ?>
<?php endif; ?>
<?php endwhile; ?>
share|improve this question
It's too late to sort at this point; you need to specify the sort parameters in your query. Here's a similar issue. – montrealist Mar 31 '13 at 6:55
Please post your query, if you want adequate help. – Johannes Pille Mar 31 '13 at 11:33

If you need sorted result by default,you need to query with sort parameters see https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters more more

(example from the codes)

$query = new WP_Query( array ( 'post_type' => 'product', 'orderby' => 'meta_value', 'meta_key' => 'price' ) );

Alternatively, you could also easily do the sorting using jquery plugins like tinysort ( see http://tinysort.sjeiti.com/ )

Hope this helps!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.