Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers 3

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.

share|improve this answer
    
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 '13 at 13:48
1  
@honk31 See my update for a way to get a specific order. –  toscho Apr 19 '13 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. –  Johannes Pille Apr 19 '13 at 13:51
add comment

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.

share|improve this answer
    
that makes no difference. –  honk31 Apr 19 '13 at 13:43
add comment

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...

share|improve this answer
    
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. –  Johannes Pille Apr 19 '13 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 '13 at 16:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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