admin管理员组文章数量:1130349
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 badges1 Answer
Reset to default 3Yes.
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.
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 badges1 Answer
Reset to default 3Yes.
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 metaAre multiple values from getpostmeta guaranteed to be ordered
版权声明:本文标题:post meta - Are multiple values from get_post_meta guaranteed to be ordered? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749156068a2324612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论