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 have this query:

  $new = new WP_Query(array('post_type'=>'support','order'=>'DESC', 'meta_key'=>'post_views_count', 'orderby'=> 'meta_value_num', 'posts_per_page'=>'20'));

the result set is pulling the correct number of posts, from the custom post type support but the posts are not ordered by the meta_value.

I cannot fathom why this is as the codex shows this is the correct way to order by a custom meta value?

share|improve this question
    
Seems like the code is right...what does var_dump($new) give you? Are you sure the spelling on your meta_key is correct? –  tollmanz Jul 19 '11 at 2:46
    
do you actually have values in the meta field post_views_count ? –  helgatheviking Feb 24 '12 at 5:38
add comment

1 Answer

I had trouble with a similar issue of sorting posts from a custom post type in a specified order on their archive page. I used http://wordpress.org/extend/plugins/sort-query-posts/.

The plugin lets you sort posts by author, comment_count, date, id, menu_order, modified, parent, or title. It only requires you to add the following lines

<?php query_posts(array('post_type'=>'post_type_name', 'posts_per_page' => 20)); ?>
<?php sort_query_posts_by('sort_type', 'order'); ?>

to your code, before the loop. Where 'sort_type' is the item by which your posts are sorted and 'order' is asc, desc, or rand. (very clear instructions in the short readme file).

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.