$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>post meta - Are multiple values from get_post_meta guaranteed to be ordered?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far