How to create pagination for users list using custom array?
After searching I didn't found any solution, So I'm wondering how can I create pagination for users list like that?
I'm highly appreciated your help, Thanks.
?php
global $wp_query;
$topuser = array();
$avatar_size = 100;
$args = array(
'role__in' = array('contributor', 'author'),
'hide_empty' = '1'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$query = get_posts( array('author' = $user-ID, 'cat' = '3', 'numberposts' = -1, 'post_type' = 'post' ));
$counter = 0;
$post_count = count_user_posts( $user-ID );
if ( ! $post_count ) {
continue;
}
// get each post of a user
foreach ( $query as $post ){
$views = absint( get_post_meta( $post-ID, 'post_views_count', true ));
$counter += $views;
}
$topuser[] = array(
'id' = $user-ID,
'views' = $counter
);
wp_reset_query();
}
// function to sort array based on views count
function sortViews($a, $b) {
return $b['views'] - $a['views'];
}
usort($topuser, 'sortViews'); // sort the array
//$output = array_slice($topuser, 0, 10); // slice the array by limit 10
$output = $topuser; // all users
$rank=0;
$rankpostcount=0;
echo 'div id="top-artists-contributors"';
foreach ($output as $user){
$rank++;
$rankpostcount++;
$query = get_posts( array('author' = $user['id'], 'cat' = '3', 'numberposts' = -1, 'post_type' = 'post' ));
$avatar = get_avatar($user['id'], $avatar_size);
$author_profile_url = get_author_posts_url($user['id']);
$profile = get_userdata($user['id']);
// update the rank for each user
update_user_meta( $user['id'], 'user_rank', $rank );
if (count($query)) {
echo 'div class="rankpostcount-'.$rankpostcount.' single-item-9"';
echo 'div class="members-name-9"a href="', $author_profile_url, '"' . $profile-first_name .'/adiv class="author-rank-9" title="Artist Rank"'.$rank.'/div/div';
echo '/div';
}
}
echo '/div';
}
?
Update 1
This is what I have tried to do but i can't access to the second page!
?php
global $wp_query;
$topuser = array();
$avatar_size = 100;
$args = array(
'role__in' = array('contributor', 'author'),
'hide_empty' = '1'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$query = get_posts( array('author' = $user-ID, 'cat' = '3', 'numberposts' = -1, 'post_type' = 'post' ));
$counter = 0;
$post_count = count_user_posts( $user-ID );
if ( ! $post_count ) {
continue;
}
// get each post of a user
foreach ( $query as $post ){
$views = absint( get_post_meta( $post-ID, 'post_views_count', true ));
$counter += $views;
}
$topuser[] = array(
'id' = $user-ID,
'views' = $counter
);
wp_reset_query();
}
// function to sort array based on views count
function sortViews($a, $b) {
return $b['views'] - $a['views'];
}
usort($topuser, 'sortViews'); // sort the array
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $topuser ); //total items in array
$limit = 20; //per page
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] = 0
$page = min($page, $totalPages); //get last page when $_GET['page'] $totalPages
$offset = ($page - 1) * $limit;
if( $offset 0 ) $offset = 0;
$topuser = array_slice( $topuser, $offset, $limit );
$rank=0;
$rankpostcount=0;
echo 'div id="top-artists-contributors"';
foreach ($topuser as $user){
$rank++;
$rankpostcount++;
$query = get_posts( array('author' = $user['id'], 'cat' = '3', 'numberposts' = -1, 'post_type' = 'post' ));
$avatar = get_avatar($user['id'], $avatar_size);
$author_profile_url = get_author_posts_url($user['id']);
$profile = get_userdata($user['id']);
// update the rank for each user
update_user_meta( $user['id'], 'user_rank', $rank );
if (count($query)) {
echo 'div class="rankpostcount-'.$rankpostcount.' single-item-9"';
echo 'div class="members-name-9"a href="', $author_profile_url, '"' . $profile-first_name .'/adiv class="author-rank-9" title="Artist Rank"'.$rank.'/div/div';
echo '/div';
}
}
echo '/div';
}
$link = 'index.php?page=%d';
$pagerContainer = 'div style="width: 300px;"';
if( $totalPages != 0 )
{
if( $page == 1 )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( 'a href="' . $link . '" style="color: #c00" #171; prev page/a', $page - 1 );
}
$pagerContainer .= ' span page strong' . $page . '/strong from ' . $totalPages . '/span';
if( $page == $totalPages )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( 'a href="' . $link . '" style="color: #c00" next page #187; /a', $page + 1 );
}
}
$pagerContainer .= '/div';
echo $pagerContainer;
?
Update 2
Trying to update users ranks meta with ajax
function update_users_ranks() {
global $wp_query;
$topuser = array();
$args = array(
'role__in' = array('contributor', 'author'),
'hide_empty' = '1'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$query = get_posts( array('author' = $user-ID, 'cat' = '3', 'numberposts' = -1, 'post_type' = 'post' ));
$counter = 0;
$post_count = count_user_posts( $user-ID );
if ( ! $post_count ) {
continue;
}
// get each post of a user
foreach ( $query as $post ){
$views = absint( get_post_meta( $post-ID, 'post_views_count', true ));
$counter += $views;
}
$topuser[] = array(
'id' = $user-ID,
'views' = $counter
);
wp_reset_query();
}
// function to sort array based on views count
function sortViews($a, $b) {
return $b['views'] - $a['views'];
}
usort($topuser, 'sortViews'); // sort the array
$output = $topuser;
$rank=0;
// Update ranks
foreach ($output as $user){
$rank++;
update_user_meta( $user['id'], 'user_rank', $rank );
}
die();
}
add_action('wp_ajax_update_users_ranks' , 'update_users_ranks');
Update 3
Here how I set and get and updating the post views.
// function to display number of posts.
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
if ($count 1000) {
return round ( $count / 1000 ,1 ).'K';
}
return $count.' ';
}
// function to count views.
function setPostViews($postID) {
$user_ip = $_SERVER['REMOTE_ADDR']; //retrieve the current IP address of the visitor
$key = $user_ip . 'x' . $postID; //combine post ID IP to form unique key
$value = array($user_ip, $postID); // store post ID IP as separate values (see note)
$visited = get_transient($key); //get transient and store in variable
//check to see if the Post ID/IP ($key) address is currently stored as a transient
if ( current_user_can('administrator') || false === ( $visited ) ) {
//store the unique key, Post ID IP address for 12 hours if it does not exist
set_transient( $key, $value, 60*60*1 );
// now run post views function
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
}
Topic list-authors php pagination Wordpress
Category Web