admin管理员组文章数量:1130349
I want to display 5 users who have published more than 10 posts, when the page loaded again, it should show another 5 users or mixed with new users with more than 10 posts.
How do I achieve this? Thank you for any help.
I want to display 5 users who have published more than 10 posts, when the page loaded again, it should show another 5 users or mixed with new users with more than 10 posts.
How do I achieve this? Thank you for any help.
Share Improve this question asked Nov 6, 2018 at 14:00 p onelandp oneland 171 silver badge8 bronze badges1 Answer
Reset to default 0I tried this code and it should work for you
In your function.php to make random order available in WP_User_Query
function my_random_user_query( $class ) {
if( 'rand' == $class->query_vars['orderby'] )
$class->query_orderby = str_replace( 'user_login', 'RAND()', $class->query_orderby );
return $class;
}
add_action( 'pre_user_query', 'my_random_user_query' );
Source
And a query like this
// Get all users id
$args = array (
'has_published_posts' => true,
'fields' => 'ids',
);
$wp_users = new WP_User_Query( $args );
$ids_users = $wp_users->get_results();
// Return users id with more than 10 posts
$users = array_filter($ids_users, function($id){
$count = count_user_posts( $id );
if($count >= 10) return $id;
});
// Get users previously filtered
$args = array (
'orderby' => 'rand',
'include' => $users,
'number' => 5
);
$wp_users = new WP_User_Query( $args );
$result = $wp_users->get_results();
Codex query about users
I want to display 5 users who have published more than 10 posts, when the page loaded again, it should show another 5 users or mixed with new users with more than 10 posts.
How do I achieve this? Thank you for any help.
I want to display 5 users who have published more than 10 posts, when the page loaded again, it should show another 5 users or mixed with new users with more than 10 posts.
How do I achieve this? Thank you for any help.
Share Improve this question asked Nov 6, 2018 at 14:00 p onelandp oneland 171 silver badge8 bronze badges1 Answer
Reset to default 0I tried this code and it should work for you
In your function.php to make random order available in WP_User_Query
function my_random_user_query( $class ) {
if( 'rand' == $class->query_vars['orderby'] )
$class->query_orderby = str_replace( 'user_login', 'RAND()', $class->query_orderby );
return $class;
}
add_action( 'pre_user_query', 'my_random_user_query' );
Source
And a query like this
// Get all users id
$args = array (
'has_published_posts' => true,
'fields' => 'ids',
);
$wp_users = new WP_User_Query( $args );
$ids_users = $wp_users->get_results();
// Return users id with more than 10 posts
$users = array_filter($ids_users, function($id){
$count = count_user_posts( $id );
if($count >= 10) return $id;
});
// Get users previously filtered
$args = array (
'orderby' => 'rand',
'include' => $users,
'number' => 5
);
$wp_users = new WP_User_Query( $args );
$result = $wp_users->get_results();
Codex query about users
本文标签: How to query 5 users in random who have published more than 10 posts
版权声明:本文标题:How to query 5 users in random who have published more than 10 posts 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749201996a2331957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论