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?
1 Answer
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.