不能按分类法查询用户。您需要重新考虑您的系统。我该怎么办?
保留分类法,但仅用于输出可选择的值-wp_dropdown_categories()保存所选值AS 用户元,NOT AS 分类法-update_user_meta()现在您可以通过该值查询用户-WP_User_Query()
Example:
此代码将转到发布作业的页面模板。
// This will check if form was submitted, it will not trigger/show on "normal" page load
if( $_SERVER[\'REQUEST_METHOD\'] == \'POST\' ) {
// This will get the profession from input after form is submitted & page reloaded
// Change "profession" to the name of that input where user can choose profession
$submitted_profession = $_POST[\'profession\'];
// Arguments for user query, see more from the user query link above
$args = array(
// How many to return?
\'number\' => 3,
// User meta
\'meta_query\' => array(
array(
\'key\' => \'profession\', // Name of user meta key in database
\'value\' => $submitted_profession, // We get this from form, see above
\'compare\' => \'=\'
)
)
);
// Query itself
$suggested_users_query = new WP_User_Query( $args );
// Get the results
$users = $suggested_users_query->get_results();
// Check if there actually are users based on that criteria
if ( ! empty( $users ) ) {
// All the output how you want your users to look like goes here
// I wrote a small example
echo \'<ul>\';
// Loop trough each user
foreach ( $users as $user ) {
// Get all the user data
// To find all the fields you could use, search "get_userdata()" online
$user_info = get_userdata( $user->ID );
echo \'<li>\' . $user_info->first_name.\' \' . $user_info->last_name . \'</li>\';
}
echo \'</ul>\';
}
// No users with that profession
else {
echo \'No users with profession \' . $submitted_profession;
}
}