admin管理员组文章数量:1289525
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 "<p>O último usuário é {$ultimousuario}</p>";
We're trying to get the last user that contains "usuario*" at it's user_login, order by decrescent.
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 "<p>O último usuário é {$ultimousuario}</p>";
We're trying to get the last user that contains "usuario*" at it's user_login, order by decrescent.
Share Improve this question edited Jul 29, 2021 at 15:53 birgire 68k7 gold badges120 silver badges252 bronze badges asked Jul 29, 2021 at 15:25 Fernando MitsugiFernando Mitsugi 11 bronze badge 4 |1 Answer
Reset to default 4If 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
get_users()
WP_User_Query::prepare_query()
for the$args
detailsWP_User::get()
to extract theuser_login
from theWP_User
object
本文标签: How to get last user with wpdb
版权声明:本文标题:How to get last user with wpdb? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741414252a2377417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
user_login
is WordPress's term for "username", so ordering by that will just get you the username that's deepest in the alphabet. – Pat J Commented Jul 29, 2021 at 15:48user_login
that's deepest in the alphabet starting withusuario
(ie, if you haveusuarioAAA
andusuarioZZZ
, and you wantusuarioZZZ
), then try changingWHERE user_login='ususario*'
toWHERE user_login LIKE 'usuario%'
. – Pat J Commented Jul 29, 2021 at 15:52