Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#25511 closed enhancement (fixed)

get_post_meta does not always return data in the same order

Reported by: mattheu Owned by: wonderboymusic
Milestone: 3.8 Priority: normal
Severity: normal Version: 3.8
Component: General Keywords: has-patch commit
Focuses: Cc:


get_post_meta does not always return data in the same order when returning an array of all meta values for a given key.

Expected outcome: I expect the order to remain the same every time this data is retrieved.

This usually returns data in the correct order but it is not guaranteed as the SQL query in the update_meta_cache function does not explicitly order by anything.

I propose that meta data should be explicitly ordered by meta_id.

I have a patch if this is considered a but

Attachments (4)

25511.diff (714 bytes) - added by mattheu 8 years ago.
25511.2.diff (675 bytes) - added by mattheu 8 years ago.
25511.3.diff (671 bytes) - added by mattheu 8 years ago.
25511.4.diff (751 bytes) - added by wonderboymusic 8 years ago.

Download all attachments as: .zip

Change History (19)

#1 @mattheu
8 years ago

Use case:

I have a plugin that allows users to add custom fields to the post edit screen. It is possible to create a field and add multiple values which are then stored as separate post meta entries using a single key. Users expect that the order of this data will be preserved.

When this data is saved, all previously stored data is deleted and then added again in the order it was submitted to ensure that the meta_id order corresponds to the desired order of the data.

#2 @jeremyfelt
8 years ago

Hi @mattheu, you mentioned having a patch in the ticket description. Can you upload this patch now so that we can see what kind of approach you had in mind?

8 years ago

#3 @mattheu
8 years ago

@jeremyfelt - patch added

#4 @mattheu
8 years ago

  • Keywords has-patch added

8 years ago

#5 @mattheu
8 years ago

I've updated the patch to use a ternary operator for the orderby_column to be more consistent with the rest of the code base. Thanks @jeremyfelt

#6 @jeremyfelt
8 years ago

  • Milestone changed from Awaiting Review to 3.8
  • Type changed from defect (bug) to enhancement

8 years ago

#7 @mattheu
8 years ago

Updated the patch so that variable name is more consitent with rest of codebase

#8 @pauldewouters
8 years ago

  • Cc pauldewouters@… added

#9 @scottsweb
8 years ago

  • Cc scottsweb added

#10 @jonathanbardo
8 years ago

  • Cc bardo.jonathan@… added

#11 @goto10
8 years ago

  • Cc dromsey@… added

#12 @wonderboymusic
8 years ago

  • Keywords commit added

25511.4.diff​ ditches the unnecessary $wpdb->prepare call which was passing a variable to a SQL statement with no replacement tokens

#13 @badconker
8 years ago

  • Cc badconker@… added

#14 @wonderboymusic
8 years ago

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

In 26307:

In update_meta_cache(), ensure that meta is always stored in the same order. Removes an unnecessary $wpdb->prepare statement. Adds unit test.

Props mattheu.
Fixes #25511.

#15 @nacin
8 years ago

#25619 was marked as a duplicate.

Note: See TracTickets for help on using tickets.