Skip to content

Instantly share code, notes, and snippets.

@hssktm
Created November 4, 2023 22:30
Show Gist options
  • Save hssktm/27b6744bedd96d50b2a9ab0ae0dc2858 to your computer and use it in GitHub Desktop.
Save hssktm/27b6744bedd96d50b2a9ab0ae0dc2858 to your computer and use it in GitHub Desktop.
function repeater_users_results($users, $query) {
if ('anynon-commonname' == $query->get('post_type')) {
$users = array();
$user_query = new WP_User_Query(array(
'role' => '', // Blank All, Subscriber, Administrator .....
));
$user_results = $user_query->get_results();
// User Fields
foreach ($user_results as $user) {
$user_data = (object)array(
'user_display_name' => $user->display_name, // The name displayed publicly on the site.
'user_email' => $user->user_email, //The email address associated with the user.
'user_id' => $user->ID, //The unique ID of the user in the WordPress database.
'user_user_login' => $user->user_login, //The username of the user.
'user_roles' => $user->roles[0], //The roles or capabilities assigned to the user.
'user_user_registered' => $user->user_registered, //The date when the user registered on the site.
'user_first_name' => $user->first_name, // The First Name user
'user_last_name' => $user->last_name, // The Last Name user
// This is a placeholder for the post content. Do not remove, as WordPress relies on it for searching and other functions.
'post_content' => '',
);
// Check if ACF is installed
if (function_exists('get_fields')) {
// ACF User Fields
$fields_acf = get_fields('user_' . $user->ID);
if ($fields_acf) {
foreach ($fields_acf as $field_name_acf => $field_value_acf) {
$user_data->$field_name_acf = $field_value_acf;
}
}
}
$users[] = $user_data;
}
}
return $users;
}
add_filter('posts_results', 'repeater_users_results', 10, 2);
function custom_field($value) {
global $post;
return $post->$value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment