Make WordPress Core

Opened 15 years ago

Closed 14 years ago

Last modified 14 years ago

#10649 closed enhancement (fixed)

Meta_key sort for numeric values / Natural Sort Order

Reported by: stencil's profile stencil Owned by: ryan's profile ryan
Milestone: 3.0 Priority: normal
Severity: normal Version: 2.8.4
Component: Query Keywords: has-patch commit
Focuses: Cc:


When trying to sort posts by meta_key with numeric values the order is not "natural".

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

This should be

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

I have attached a simple addition to wp-includes/query.php which allows for this type of sorting.

Query use: orderby=meta_value_num

Attachments (3)

query.php.diff (585 bytes) - added by stencil 15 years ago.
Addition of meta_value_num
query.php.2.diff (1.5 KB) - added by scribu 14 years ago.
check allowed_keys earlier, remove unnecessary comments
query.php.3.diff (1.6 KB) - added by scribu 14 years ago.
remove_redundant check, use foreach instead of for

Download all attachments as: .zip

Change History (10)

15 years ago

Addition of meta_value_num

#1 @stencil
14 years ago

  • Milestone changed from Future Release to 2.9.3
  • Priority changed from normal to high

Any chance of pushing this fix please? It keeps annoying me each time I upgrade WP.

or is there a blocking issue?

#2 @dd32
14 years ago

  • Keywords dev-feedback has-patch added
  • Milestone changed from 2.9.3 to 3.0

Moving to 3.0, as 2.9 is not for new enhancements/feature requests.

Perhaps another dev who's more database savy than I can comment on any particular issues which this might cause? Whilst i see none, Would be handy to know that for sure.

#3 @stencil
14 years ago

I think this is really a bug, simply because sorting posts via a numerical meta_key fails to work when values are > 9.

The addition of meta_value_num would be a new feature, yes, but not really required just to fix this bug (which is in the DB query).

#4 @scribu
14 years ago

  • Keywords commit added; dev-feedback removed
  • Priority changed from high to normal

Patch looks good.

14 years ago

check allowed_keys earlier, remove unnecessary comments

14 years ago

remove_redundant check, use foreach instead of for

#5 @scribu
14 years ago

Since we're here, I've made some straightforward cleanups in query.php.3.diff

#6 follow-up: @dd32
14 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [13501]) Add meta_value_num meta_key ordering for WP_Query. Allows Natural numeric ordering of results. Props stencil & scribu for cleanups. Fixes #10649

#7 in reply to: ↑ 6 @stencil
14 years ago

Nice to see the commit, cheers

Note: See TracTickets for help on using tickets.