Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

<table>
<?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; ?>
   </table>
share|improve this question
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. –  dalbaeb Mar 31 '13 at 6:55
1  
Please post your query, if you want adequate help. –  Johannes Pille Mar 31 '13 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!

share|improve this answer

Your Answer

 
discard

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.