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 can't believe I'm asking this question. It feels like such a simple method, yet I've poured over the WP pages and dozens of similar questions to no avail, so here it is...

I've got a WP_Query with the following, that just gets the last 5 posts:

$args = Array(
  'post_type' => 'post'
  'posts_per_page' => '5'

All I wanted to do was order the last 5 posts by the meta_value_num of the meta_key 'views'. But instead, when I put in something like the following, it grabs the top five most viewed posts ever.

$args = Array(
  'post_type' => 'post'
  'posts_per_page' => '5'
  'meta_key' => 'views';
  'orderby' => 'meta_value_num'

What am I doing wrong? I feel like this is endlessly frustrating, probably more so because I know the solution is almost certainly simple as pie.

share|improve this question
    
Try 'order' = 'desc'. Actually you'd have to use 'orderby' = 'date' and 'order' = 'desc', but you need ordering by meta key and unfortunately you cannot use 2 orderby values! Or try the date orderby with a meta query. –  Rutwick Gangurde Dec 30 '11 at 20:44
add comment

1 Answer

You need last 5 posts sorted based on the meta_key 'views'? In other words you need to sort in descending order so the last 5 posts will be on top. did you try the following args?

$args = Array(
  'post_type' => 'post',
  'posts_per_page' => '5'.
  'meta_key' => 'views',
  'orderby' => 'meta_value_num',
  'order' => 'DESC'
);
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.