If I insert multiple custom fields to my article, they get sorted in “I don’t know what” way. So when I insert some multiple fields in an article and call those via
$cf = get_post_custom();
foreach ($cf['ISSUU-configID'] as $key => $value) {
echo "<h4>" . $cf['ISSUU-configID'][$key] . "</h4>\n";
}
their order is mixed up and neither logically ordered nor in the way I inserted them. Seems weird to me.
To make it a little clearer:
I insert these numbers, each one in an own custom field (in specifically that order):
12345, 23456, 34567, 45678
And the output will be something like (in fact this is always different):
45678, 23456, 12345, 34567
3 Answers
The SQL query triggered by get_post_custom()
is in update_meta_cache()
and it looks like this:
$wpdb->get_results(
$wpdb->prepare(
"SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)",
$meta_type
),
ARRAY_A
);
So there is no ORDER BY
given.
Without ORDER BY
the resulting order is almost unpredictable: it depends on the optimizer, used indexes and their order.
The order the fields are saved doesn’t matter here.
You can filter 'query'
and add an ORDER BY
clause to the query. See wp-includes/wp-db.php
– wpdb::query()
for details.