i’m using custom fields in wordpress to store meta values. some custom fields have multiple values. i’m retrieving the array with “get post meta” which returns an array, as expected. but it seems the different items are ordered without any logic. some show up in the order they were entered, other’s in opposite order, some total chaos.. what could i be missing?
i can’t change the way the items are stored anymore.. there is too many entries in the database and the values show up perfect in the editing area but are stored in different order within the array.
This may or may not be possible. WordPress is a bit like free energy machines – it violates a few useful concepts.
All your metadata is stored in the
postmeta
table. This table has a few fields: meta ID, post ID, meta key, value. Every time you add a meta, you add a row to this table. Every time you update a meta, however, you do not change the row order.get_post_meta
usually returns rows in the same order, so I am guessing you are doing some sort of sorting somewhere. Could we see some code? If it’s always in ascending or descending but never as a random mix, you have asort()
lost somewhere.I just recently had this same issue, here’s what I found:
When you use
get_post_meta()
, WordPress checks if the meta data for the given object (post) has alreadby been loaded; if it has, then it’s on the object cache (volatile, apc, memcached, etc) and load it’s from there.If the meta data it’s not on the object cache, then it loads all the meta data for that object through
update_meta_cache()
, which queries the database without anORDER BY
If your storage engine it’s MyISAM, the results will be returned on a random order every time you update the meta data, but when using InnoDB the order in which the results are returned seems to be consistent (at least on my tests)