WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#10649 closed enhancement (fixed)

Meta_key sort for numeric values / Natural Sort Order

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

Description

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 5 years ago.
Addition of meta_value_num
query.php.2.diff (1.5 KB) - added by scribu 4 years ago.
check allowed_keys earlier, remove unnecessary comments
query.php.3.diff (1.6 KB) - added by scribu 4 years ago.
remove_redundant check, use foreach instead of for

Download all attachments as: .zip

Change History (10)

stencil5 years ago

Addition of meta_value_num

comment:1 stencil4 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?

comment:2 dd324 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.

comment:3 stencil4 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).

comment:4 scribu4 years ago

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

Patch looks good.

scribu4 years ago

check allowed_keys earlier, remove unnecessary comments

scribu4 years ago

remove_redundant check, use foreach instead of for

comment:5 scribu4 years ago

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

comment:6 follow-up: dd324 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

comment:7 in reply to: ↑ 6 stencil4 years ago

Nice to see the commit, cheers

Note: See TracTickets for help on using tickets.