You shouldn’t be passing in $users["results"]
directly to a foreach without checking that $users
is an array, and that it has a ‘results’ key. What if $users
is empty, or false, and a WP_Error
object?
In this case, it isn’t an array, it’s a WP_User_Query
object. Casting it to an array won’t fix that. Instead you should be using it like this:
$user_query = new WP_User_Query( $args );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
} else {
echo 'No users found.';
}
Where $user_query->results
is an array in the $user_query
object
Not like this:
// this will fail!
$user_query = new WP_User_Query( $args );
$user_query = ( array ) $user_query;
foreach ( $user_query["results"] as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
Further reading