How to get last user with wpdb?

can someone please help me with this query:

$ultimousuario = $wpdb-get_var( SELECT TOP 1 FROM  $wpdb-users WHERE user_login = 'usuario*' ORDER BY user_login DESC);
echo pO último usuário é {$ultimousuario}/p;

We're trying to get the last user that contains usuario* at it's user_login, order by decrescent.

Topic select wpdb users Wordpress

Category Web


If you're trying to get the last user created with the username format usuario*, I'd recommend using get_users() instead of writing a SQL query.

$args = array(
    "search"         => "usuario*",            // Search string
    "search_columns" => array( "user_login" ), // Field(s) to search
    "order"          => "DESC",                // Search order
    "orderby"        => "user_registered",     // Order by this
    "number"         => 1,                     // Get only this many users
);
$user_objects = get_users( $args );
// Ensure we've got results
if ( ! empty( $user_objects ) ) {
    // Pull the 1st array element, which will be a WP_User object,
    // and use the get() method to extract the username to a variable
    $most_recent_usuario = $user_objects[0]->get( 'login_name' );
}
echo "<p>O último usuário é {$most_recent_usuario}</p>";

References

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.