get_user_meta方法介绍

原文:Function Reference/get user meta

方法简述

将给定用户的单个属性字段或者所有属性字段从user_meta表查询出来。也曾可以使用get_metadata()。这个方法替代了已经废弃的get_usermeta()方法。

如何使用

<?php get_user_meta($user_id, $key, $single);  ?>

参数

$user_id

(integer) (required) 要查询的用户ID。
默认: None

$key
(string) (optional) 存储在wp_usermeta表中的指向要返回的配置值meta_key的键。如果设置为空,会返回指定用户的所有配置属性。。
默认: (empty string)

$single
(boolean) (optional)设置为true则返回指定用户的属性值,如果为false则返回一个数组。在$key 参数设置为空的情况下$singe设置为何值不影响结果。
默认: false

返回值

(mixed)
如果$key参数未设置或者$singlefalse,则返回一个数组。如果$single设置为true则返回一个meta_value属性值。

注意
如果这个属性值不存在, 且$single设置为true那将返回一个空字符串。如果$single设置为false那么将返回一个空的数组。

示例

这个示例会返回用户ID为9的姓氏并展示出来。

<?php 
  $user_id = 9;
  $key = 'last_name';
  $single = true;
  $user_last = get_user_meta( $user_id, $key, $single ); 
  echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>'; 
?>

用户ID为9的姓氏为Franklin

获取所有配置属性

这个例子示范的是当设置$key这个参数为空,来获取指定用户的所有配置属性(这个例子指定用户ID为9):

<?php
  $all_meta_for_user = get_user_meta( 9 );
  print_r( $all_meta_for_user );
?>

结果:

Array ( [first_name] => Array ( [0] => Tom ) [last_name] => Array ( [0] => Auger) [nickname] => Array ( [0] => tomauger ) [description] => etc.... )

注意: 为了能够使用例子中返回的数据,你必须去顺着返回的数据的顺序来一个个的访问数组的第一个值,像这样:

$last_name = $all_meta_for_user['last_name'][0];

为了避免这么麻烦,你可以使用这个简单的array_map方法,来获取到每个数组结果的第一个值(这样其实是模拟了$single这个参数的功能,在$key参数设置了的前提下):

  $all_meta_for_user = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );

  print_r( $all_meta_for_user );

结果:

Array ( [first_name] => Tom [last_name] => Auger [nickname] => tomauger [description] => etc.... )

另外,如果你想获取用户所有的配置属性,并且过滤掉空值,可以使用array_filter方法来进行过滤:

// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );

// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
    return $a[0];
}, $meta ) );

提示

如果要获取的值是存在的,但它是一个空值,它会返回一个空字符串或者是数组,就好像这个配置属性值不存在一样。这个现象可能会引发一些异常的表现,也就是当你设置空的用户配置属性值的时候。这个时候就可以尝试使用add_user_meta来替代update_user_meta来防止这种设置空值的现象。我认为get_user_meta应该对于不存在的配置属性返回空值会更合适一点。

源文件

get_user_meta()看这里 wp-includes/user.php.

相关

add_user_meta(), delete_user_meta(), get_user_meta(), update_user_meta(), get_user_option(), delete_user_option(), update_user_option(),

Function Reference 索引看这里and Template Tags索引看这里.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码实现: ``` static GstPadProbeReturn src_pad_buffer_probe(GstPad *pad, GstPadProbeInfo *info, gpointer user_data) { GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info); GstMeta *meta; GstCustomMeta *custom_meta; gchar *data = "附加的字符串"; meta = gst_buffer_get_custom_meta(buffer, gst_custom_meta_api_get_type()); if (!meta) { custom_meta = gst_buffer_add_custom_meta(buffer, gst_custom_meta_api_get_type(), NULL); } else { custom_meta = (GstCustomMeta *) meta; } custom_meta->data = data; custom_meta->size = strlen(data); return GST_PAD_PROBE_OK; } static GstPadProbeReturn next_plugin_src_pad_buffer_probe(GstPad *pad, GstPadProbeInfo *info, gpointer user_data) { GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER(info); GstMeta *meta; GstCustomMeta *custom_meta; meta = gst_buffer_get_custom_meta(buffer, gst_custom_meta_api_get_type()); if (meta) { custom_meta = (GstCustomMeta *) meta; g_print("附加的字符串是:%s\n", custom_meta->data); } return GST_PAD_PROBE_OK; } // 在pipeline中添加probe GstPad *src_pad = gst_element_get_static_pad(src_element, "src"); GstPad *next_plugin_src_pad = gst_element_get_static_pad(next_plugin_element, "src"); gst_pad_add_probe(src_pad, GST_PAD_PROBE_TYPE_BUFFER, src_pad_buffer_probe, NULL, NULL); gst_pad_add_probe(next_plugin_src_pad, GST_PAD_PROBE_TYPE_BUFFER, next_plugin_src_pad_buffer_probe, NULL, NULL); ``` 这段代码实现了在 `src_element` 的 `src` pad 上添加一个 buffer 探针,在 buffer 中附加一个字符串,并在 `next_plugin_element` 的 `src` pad 上添加一个 buffer 探针,获取附加的字符串并打印出来。其中,`gst_custom_meta_api_get_type()` 是自定义 meta 的类型,需要先注册。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值