post meta - Are multiple values from get_post_meta guaranteed to be ordered?

admin2025-06-06  2

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?

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?

Share Improve this question asked Oct 10, 2017 at 10:11 FlimmFlimm 7307 silver badges26 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749156092a316842.html

最新回复(0)