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 need help in sorting lists of posts inside a static page by a custom field value. I'm currently using the code below but it is not sorting correctly.

<?php
            $postslist = get_posts('numberposts=15&order=DESC&meta_key=sorting&orderby=meta_value_number&tag='. get_post_meta($post->ID, 'Tag', true));
            foreach ($postslist as $post) :
                setup_postdata($post);
            ?>

Any help would be appreciated.

share|improve this question
add comment

2 Answers

See the answers to this question: WordPress how to sort by meta value?

share|improve this answer
add comment

You Should use meta_key_num instead of meta_key_number, so args for get_posts would be like this:

    <?php

$post_list_args = array(
   'numberposts' => 15,
   'order' => 'DESC',
   'orderby' => 'meta_key_num',
   'meta_key' => 'sorting',
   'tag' => get_post_meta($post->ID, 'Tag', true),
);

$post_list = get_posts( $post_list_args );
foreach ( $post_list as $post ) :
   setup_postdata( $post );

?>
share|improve this answer
add comment

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.