admin管理员组文章数量:1122846
I am attempting to get meta data in a list of WP users using this code, but am unable to retrieve the meta data:
$args = array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$user_id = $user->ID;
$post_count = count_user_posts($user_id);
if( $post_count > 0 ){
$user_meta = get_user_meta( $user_id );
echo ' ID: ' . $user_id . ' | ';
var_dump($user_meta);
echo '<br />';
}
}
This is what the script returns:
ID: 6460 | array(0) { } ID: 6468 | array(0) { } ID: 6472 | array(0) { } ID: 6474 | array(0) { } etc...
However, when I try to fetch the user meta data outside of the loop, it works fine:
$user_id = 6460;
var_dump(get_user_meta( $user_id ));
Any idea what I'm doing wrong?
I am attempting to get meta data in a list of WP users using this code, but am unable to retrieve the meta data:
$args = array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$user_id = $user->ID;
$post_count = count_user_posts($user_id);
if( $post_count > 0 ){
$user_meta = get_user_meta( $user_id );
echo ' ID: ' . $user_id . ' | ';
var_dump($user_meta);
echo '<br />';
}
}
This is what the script returns:
ID: 6460 | array(0) { } ID: 6468 | array(0) { } ID: 6472 | array(0) { } ID: 6474 | array(0) { } etc...
However, when I try to fetch the user meta data outside of the loop, it works fine:
$user_id = 6460;
var_dump(get_user_meta( $user_id ));
Any idea what I'm doing wrong?
Share Improve this question asked Jun 3, 2024 at 20:59 zeldatronzeldatron 11 Answer
Reset to default 0Use this code instead-
$args = array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$user_id = $user->ID;
$post_count = count_user_posts( $user_id );
if ( $post_count > 0 ) {
$user_meta = get_user_meta( $user_id, '', true );
echo 'ID: ' . $user_id . ' | ';
var_dump( $user_meta );
echo '<br />';
}
}
Ensure the get_user_meta
function retrieves all meta keys by passing an empty string as the meta key and setting the third parameter to true
to return the data in a single array.
本文标签: user metawpusermeta doesn39t return data in a foreach loop
版权声明:本文标题:user meta - wp_user_meta doesn't return data in a foreach loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304564a1932278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论