如果我创建一个数组并将其转换为JSON,如果我逐个调用12个元字段,或者调用JSON,解析它,然后得到变量,会更快吗?
几乎没有区别。元API意味着每个post最多一次将所有元键/字段/值从数据库拉入内存。因此:
$keys = [ \'this\', \'that\', \'then\' ];
$data = [];
foreach ( $keys as $key )
$data[ $key ] = get_post_meta( $post->ID, $key, true );
。。。效率与:
$json = get_post_meta( $post->ID, \'json\', true );
$data = json_decode( $json );
。。。嗯,略微优化(减少
get_post_meta
开销-我指的是PHP处理、WordPress过滤器等,而不是数据库查询):
$meta = get_post_meta( $post->ID ); // No key, all metadata returned
$data = [];
foreach ( $keys as $key ) {
$data[ $key ] = isset( $meta[ $key ] ) ? maybe_unserialize( $meta[ $key ][0] ) : null;
}