Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Order by meta value DESC - weird numerical ordering WordPress

  • SOLVED

Hello,

I've been running a blog for which I built for a few months now, and this problem has only just become apparent.

I am using a plugin which counts the number of times a post has been viewed - and puts this count into a custom field called 'views'

I am then using this query below which is meant to display the posts in order of most views. With the highest count first.


'orderby' => 'meta_value',
'meta_key' => 'views',
'post_type' => 'post',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => 10



Please see below an example of meta value 'views' from posts, and how they should be ordered.

<strong>
987
542
343
234
221
202
172
128
103
97
74
56
51
43
39
33
26
21
18
13
9
6
4
2
1
</strong>


But weirdly, my above query is ordering them like this... which is not right.

<strong>
987
97
9
74
6
56
542
51
43
4
39
343
33
26
234
221
21
202
2
18
172
13
128
103
1
</strong>


I can see why this is happening and the pattern occurring, but I cannot figure out why the query would order it like this.


Please help. I will pay full amount to person who posts a fix.


Thanks
Josh

Answers (1)

2012-06-07

John Cotton answers:

Have you tried

'orderby' => 'meta_value_num'

?


Josh Cranwell comments:

Bingo! Thanks John