WP_Query last five posts, simply ordered by meta_value

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:

Read More
$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.

Related posts

Leave a Reply

1 comment

  1. You need to sort in descending order so the last 5 posts will be on top. Just add 'order' => 'DESC' to your args.

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