参数
- $post_id
-
(int) (必填) 文章 ID
- $key
-
(string) (可选) 要检索的meta键名,默认情况下,返回所有键的数据。
默认值: ''
- $single
-
(bool) (可选) 是否返回单个值。如果未指定
$key
,此参数无效。默认值: false
返回
(mixed) 如果$single
为false,则返回值的数组。如果$single
为true,则为meta字段的值。对于无效的$post_id
(非数字、零值或负值),为False。如果传递了有效但不存在的post ID,则为空字符串。
说明
- 请注意,如果数据库排序不区分大小写(有_ci后缀),那么update_post_meta 和delete_post_meta 和get_posts() 将更新/删除/查询键名为大写或小写的meta记录,但是 get_post_meta 显然会因为 WordPress 的缓存而区分大小写。详见 https://core.trac.wordpress.org/ticket/18210,注意不要混淆大小写。。
- 使用get_metadata() 检索meta数据
源码
更新日志
版本 | 描述 |
---|---|
1.5.0 | 开始引入 |
使用示例
返回值当找不到meta字段时,如果对于给定的$post_id找不到具有给定键的meta字段,则返回值会有所不同:
如果$single为true,则返回空字符串
如果$single为false,则返回空数组。由于两者的计算结果都为false,因此可以在条件中直接使用get_post_meta,如下所示:
if( ! get_post_meta( '1', 'non-existing_meta', true ) ) {} if( ! get_post_meta( '1', 'non-existing_meta', false ) ) {} // both ifs will get run if no meta field is found; since // array() == false and '' == false
如果我想存储一个空字符串怎么办
如果出于某种原因,您需要将空字符串或数组存储到meta字段中,则在检查给定meta字段是否存在时,get_post_meta将不可靠
在这种情况下,可以使用get_post_custom_keys执行此操作:if( ! in_array( 'given_key', get_post_custom_keys( '1' ) ) ) {} // this correctly checks for the existence of the given key, // even if it's empty or has a value that evaluates as false.
此函数最好的地方是您不再需要使用它:)
由于r21559(v3.5),您只需调用
$post->foo
即可获取与get_post_meta( $post->ID, 'foo', true )
相同的内容。你甚至可以扩展它,引入动态生成的字段,因此你可以调用
echo esc_html( $post->bar )
而不是$bar = some_custom_logic( get_post_meta( $post->ID, 'bar', true ) ); echo esc_html( $bar )
.这使得代码更加清晰,可读性更强。
add_filter( 'get_post_metadata', 'add_dynamic_post_meta', 10, 4 ); /** * Add dynamically-generated "post meta" to `WP_Post` objects * * This makes it possible to access dynamic data related to a post object by simply referencing `$post->foo`. * That keeps the calling code much cleaner than if it were to have to do something like * `$foo = some_custom_logic( get_post_meta( $post->ID, 'bar', true ) ); echo esc_html( $foo )`. * * @param mixed $value * @param int $post_id * @param string $meta_key * @param int $single @todo handle the case where this is false * * @return mixed * `null` to instruct `get_metadata()` to pull the value from the database * Any non-null value will be returned as if it were pulled from the database */ function add_dynamic_post_meta( $value, $post_id, $meta_key, $single ) { $post = get_post( $post_id ); if ( 'page' != $post->post_type ) { return $value; } switch ( $meta_key ) { case 'verbose_page_template': $value = "The page template is " . ( $post->_wp_page_template ?: 'not assigned' ); break; } return $value; }
显示循环中指定键的第一个值
$key_1_value = get_post_meta( get_the_ID(), 'key_1', true ); // Check if the custom field has a value. if ( ! empty( $key_1_value ) ) { echo $key_1_value; }
默认用法获取当前文章所有键名的meta数据:
<?php $meta = get_post_meta( get_the_ID() ); ?>
获取当前文章的单个键名的所有meta值:
<?php $key_1_values = get_post_meta( get_the_ID(), 'key_1' ); ?>
获取当前文章的meta键名的第一个值:
<?php $key_1_value = get_post_meta( get_the_ID(), 'key_1', true ); ?>
检索自定义字段缩略图Url,当您在WordPress循环中时,可以使用此代码检索自定义字段。在本例中,缩略图url位于名为“thumb”的自定义字段中。
<?php if ( get_post_meta( get_the_ID(), 'thumb', true ) ) : ?> <a href="<?php the_permalink() ?>" rel="bookmark"> <img class="thumb" src="<?php echo esc_url( get_post_meta( get_the_ID(), 'thumb', true ) ); ?>" alt="<?php the_title_attribute(); ?>" /> </a> <?php endif; ?>
当您在get_post_meta中未指定 $key (”) 并将$single设置为true时,它将返回所有仍带有值数组的键。
$meta = get_post_meta(get_the_ID(), '', true); print_r($meta); //Array ( [key_1] => Array ( [0] => value_1 ), [key_2] => Array ( [0] => value_2 ) )
如果要在自定义字段中隐藏postmeta键,请在键名称前面加下划线。
调用所有post meta时,它将返回数组值:
$post_metas = get_post_meta(get_the_ID());
样本输出:
array(2) { ["_meta_key1"]=> array(1) { [0]=> "value1" } ["_meta_key2"]=> array(1) { [0]=> "val2" } }
我们可以将其转换为字符串,array_column将生成的默认数组值转换为字符串,当然我们需要array_combine来重新形成数组键:
$post_metas = get_post_meta(get_the_ID()); $post_metas = array_combine(array_keys($post_metas), array_column($post_metas, '0'));
现在输出如下:
array(2) { ["_meta_key1"]=> string(6) "value1" ["_meta_key2"]=> string(4) "val2" }
Post meta键名区分大小写
使用元键显示单个元值
<?php $meta_print_value=get_post_meta(get_the_ID(),'conference_speaker_business',true); echo($meta_print_value); ?>
如果
$key
参数的值为falsy,则即使$single
设置为true
,get_post_meta
也将返回整个post meta数组。例如:get_post_meta( $post_id, FALSE, TRUE); //Returns all the post meta fields as an array. get_post_meta( $post_id, '0', TRUE); //Will also return all the post meta fields as an array.
功能在post中检索数字视图
/** Pass in function 1: PostID => use get_the_ID(); 2: Meta Key Name => 'you can called anythings' 3: Get The Post Meta Field => get_post_meta(); 4: The Number Start Count => add anyNumber ( 0,1,100,1000 or 2000 ) 5: Count +1 6: Called Function in anypage => <?php echo relationscode_save_post_views( ) ?> but you should remove (adjacent_posts_rel_link_wp_head) **/ function relationscode_post_views( ) { $postID = get_the_ID(); $metaKey = 'relationscode_post_views'; $views = get_post_meta( $postID, $metaKey, true ); $count_start_num = 0; $count = ( empty( $views ) ? $count_start_num : $views ); $count++; if(is_single()) { update_post_meta( $postID, $metaKey, $count ); echo $count; } else { echo $views; } } remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
请注意,要添加或设置post meta数据,可以使用函数
add_post_meta
:add_post_meta( $post_id, $meta_key, $value );
这里有一个陷阱需要避免…
register_post_meta()允许您定义meta字段的默认值。如果默认值的计算结果为true,您会感到困惑,为什么还没有设置值时get_post_meta()会返回true,或者只使用delete_post_meta()。
显然,get_post_meta()将返回register_post_meta()中定义的默认值。
<?php register_meta( 'post', 'sample_field',[ 'single' => true, 'type' => 'boolean', 'default' => true, ]); if(!get_post_meta($post_id, 'sample_field', true)){ //This will now always execute unless you manually update the sample_field meta to false }