我知道get_post_custom()
, 但这将返回所有自定义字段,包括带下划线的字段。是否有一种合理的方法可以只获取没有前导下划线的内容?我会不得不求助于使用get_post_custom()
加上一个正则表达式来解析结果?
有没有一种(明智的)方法来获取帖子的所有定制字段,这些字段没有前导下划线?
2 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成
您可以编写自己的函数在SQL级别进行匹配,获取所有键并在PHP中处理它们。你不应该需要正则表达式。简单的字符串函数应该可以做到这一点echo $meta[\'key\'];get_post_meta
我怀疑#1会比#2快,特别是考虑到内置缓存#考虑到相同的内置缓存,3的性能应该与#2相当。
我不知道这些选项有什么不“理智”的地方。
SO网友:Charles Clarkson
这是根据中的示例改编的get_post_custom_keys().
function get_public_custom_fields( $post_id = 0 ) {
if ( ! empty( $post_id ) )
$custom_fields = get_post_custom( $post_id );
else
$custom_fields = get_post_custom();
$public_custom_fields = array();
foreach ( $custom_fields as $key => $value ) {
if ( \'_\' != $key{0} )
$public_custom_fields[$key] = $value;
}
return $public_custom_fields;
}
结束