1

You can create multiple rows in the postmeta table for a post with the same key, like this:

add_post_meta( $post_id, "example_key", "value1" );
add_post_meta( $post_id, "example_key", "value2" );

To retrieve the values, you can use get_post_meta, which will return an array:

$rv = get_post_meta( $post_id, "example_key" );
// $rv is array( "value1", "value2" )

Is the resulting array guaranteed to be ordered? In what order are the values in? Alphabetical, or date inserted?

2

Yes.

get_post_meta() uses get_metadata() which in turn uses update_meta_cache() to retrieve the values. In the source code we see this part (comment mine)

// $id_column is 'meta_id'
$meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A );

So the meta values will be ordered by meta_id in ascending order. Meaning, as @Jacob Peattie pointed out in comments, they will be ordered by the date they were added.

  • 1
    Small addition for clarity: This would then also be the order they were added. – Jacob Peattie Oct 10 '17 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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