Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#25511 closed enhancement (fixed)

get_post_meta does not always return data in the same order

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

Description

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 13 years ago.
25511.2.diff (675 bytes ) - added by mattheu 13 years ago.
25511.3.diff (671 bytes ) - added by mattheu 13 years ago.
25511.4.diff (751 bytes ) - added by wonderboymusic 13 years ago.

Download all attachments as: .zip

Change History (19)

#1 @mattheu
13 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
13 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?

@mattheu
13 years ago

#3 @mattheu
13 years ago

@jeremyfelt - patch added

#4 @mattheu
13 years ago

  • Keywords has-patch added

@mattheu
13 years ago

#5 @mattheu
13 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
13 years ago

  • Milestone Awaiting Review3.8
  • Type defect (bug)enhancement

@mattheu
13 years ago

#7 @mattheu
13 years ago

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

#8 @pauldewouters
13 years ago

  • Cc pauldewouters@… added

#9 @scottsweb
13 years ago

  • Cc scottsweb added

#10 @jonathanbardo
13 years ago

  • Cc bardo.jonathan@… added

#11 @goto10
13 years ago

  • Cc dromsey@… added

#12 @wonderboymusic
13 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
13 years ago

  • Cc badconker@… added

#14 @wonderboymusic
13 years ago

  • Owner set to wonderboymusic
  • Resolutionfixed
  • Status newclosed

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
13 years ago

#25619 was marked as a duplicate.

Note: See TracTickets for help on using tickets.