2

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 3

2

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.phpwpdb::query() for details.

3
  • so there is no way to order those, am i right? would be neat, if they would be saved, the way i enter them..
    – honk31
    Apr 19, 2013 at 13:48
  • 1
    @honk31 See my update for a way to get a specific order.
    – fuxia
    Apr 19, 2013 at 13:51
  • 1
    @honk31 Have you considered saving all numbers as an array and as one single metavalue? You would not have to worry about (de-)serialization, get_post_meta and update_post_meta would handle that for you. Apr 19, 2013 at 13:51
1

You can call custom fields individually via the get_post_meta function:

$meta = get_post_meta( $post_id, $key, $single ) //last argument is Boolean to return a single object or an array

More info about it here. This way you can call your custom fields how ever you want.

1
  • that makes no difference.
    – honk31
    Apr 19, 2013 at 13:43
0

thanks to mr. pille for the push in the right direction. i created a new custom field with multiple values devided by "|" and use php explode to get my values:

$cf = get_post_custom();
if ($cf['ISSUU-multiID'][0]) {
  $ids = explode("|", $cf['ISSUU-multiID'][0]);
  foreach ($ids as $key => $id) {
    echo "<h4>" . $id . "</h4>\n";
  }
}

...since i shall not edit any wp core files... thanks for the help...

2
  • While you're obviously in the clear if you have found a solution that works and satisfies you, a manual explode of a concatenated string was not exactly what I meant - update_post_meta will simply accept a regular array as the value argument. Also I don't think you quite grasped what toscho was suggesting - he wasn't proposing editing the core. Apr 19, 2013 at 14:41
  • nope. not core, you are right. but im far from being a php coder, and i once tried to get my head around php :: stuff (i guess its called classes) and i totally failed. but could you explain to me, how a value of your solution would look like? examples make the world so much easier..
    – honk31
    Apr 19, 2013 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.