@@ -2033,6 +2033,7 @@

@@ -2056,6 +2057,9 @@

This is a direct edit to a core file: /wp-includes/query.php Note the plus signs in the above code indicate new lines to add.

Author Notes:

A client wanted me to setup a custom field called “Guide Rank” which allowed them to assign #1 – 20 for a list of Bars they were posting about.

After running the posts query I found that the meta_value was being treated as a string and as such the sort order was jumbled:

eg. 1, 10, 2, 3css-tricks.comC 7 , 8 , 9

To get WordPress/MySQL to use “Natural Sort Order” you just need to apply +0 to the field name and it’ll be treated as a number (eg. meta_value+0).

So that existing behavior is not interrupted I’ve just added the new type ‘meta_value_num’.

My query line now looks like:

Which returns: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

This is up for inclusion in the WordPress trunk – so hopefully once it gets applied there should be no need for manually editing the file.