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; ?>
  • 2
    It's too late to sort at this point; you need to specify the sort parameters in your query. Here's a similar issue. Mar 31, 2013 at 6:55
  • 1
    Please post your query, if you want adequate help. Mar 31, 2013 at 11:33

1 Answer 1


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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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