我是定制wordpress的初学者,但在PHP编码方面有一些经验。
我正在努力弄清楚如何为所有具有角色的用户显示所有现有的元键(由ACF定义的自定义字段);作者;。默认字段显示正常,但无法获取这些元键/自定义字段的值。
下面的代码是使用wp generator创建并手动展开的。
我在这里尝试了两个默认字段(用户电子邮件和显示名称)和两个元自定义字段(医疗诊断和性别),但实际上我需要一个代码来获取特定的元自定义字段,另一个版本用于获取所有非空的默认字段和元字段。
如果有人有时间看❤️
$args = array(
\'role\' => \'author\',
\'order\' => \'DESC\',
\'orderby\' => \'post_count\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'medical_diagnosis\',
\'compare\' => \'EXISTS\',
),
array(
\'key\' => \'gender\',
\'compare\' => \'EXISTS\',
),
),
\'who\' => \'authors\',
\'count_total\' => true,
\'fields\' => array( \'user_email\', \'display_name\'),
);
// The User Query
$users_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $users_query->results ) ) {
foreach ( $users_query->results as $user ) {
// do something
$metadata = get_user_meta($user->ID);
echo the_author_meta( \'gender\', $user->ID );
foreach($metadata as $meta_key => $meta_value) {
echo $meta_key.\': \'.$meta_value.\'<br />\';
}
$out = \'<p>users list:</p>\'. $user->user_email . \'<br />\'. $user->display_name . \'<br />\'. $metadata[\'gender\'] . \'<br />\'. $medical_diagnosis. \'<br />\';
return $out;
}
} else {
echo \'no users found\';
}
SO网友:Vesna Mačković
已解决。字段数组中缺少ID字段。
$args = array(
\'role\' => \'author\',
\'order\' => \'DESC\',
\'orderby\' => \'post_count\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'medical_diagnosis\',
\'compare\' => \'EXISTS\',
),
array(
\'key\' =\'gender\',
\'compare\' => \'EXISTS\',
),
),
\'who\' => \'authors\',
\'count_total\' => true,
\'fields\' => array( \'ID\', \'user_email\', \'display_name\'),
);
// The User Query
$users_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $users_query->get_results() ) ) {
foreach ( $users_query->get_results() as $user ) {
// do something
// get all user meta at once:
$meta_for_user = get_user_meta( $user->ID);
$out = \'<p>users list</p>\'. $user->user_email . \'<br />\'. $user->display_name . $meta_for_user[\'medical_diagnosis\'][0] . \'<br />\' . $meta_for_user[\'gender\'][0] . \'<br />\'. $user->ID . $meta_for_user[\'last_name\'][0] . \'<br />\';
return $out;
}
} else {
echo \'no users found\';
}
} );