Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use a function to update data (likes, +1, views, point, posts..v.v.) for all members on site Once Daily.

So it may be can not run with >2000 members and >5000 posts. Only display a blank page. The function can not finish..

Can you help me for it work? Thank you!

function update_user_data() {
global $wpdb;
$topuser = array();
// query all posts by each user
$users = $wpdb->get_results("SELECT ID FROM $wpdb->users ORDER BY ID"); 
foreach ( $users as $user ) {       
    $query = get_posts( array('author' => $user->ID, 'posts_per_page' => -1));
$tong_bai_viet = 0; // Posts count
    $tong_luot_xem = 0; // Views count
$tong_luot_likes = 0; // Likes count
$tong_luot_comments = 0; // Comments count
$tong_google_plus = 0; // +1 count

// get each post of a user
if ($query) {
foreach ( $query as $post ){
    $tong_bai_viet++;
    $luot_xem = absint( get_post_meta($post->ID, 'post_views_count', true) );
    $tong_luot_xem += $luot_xem;
    $luot_like = absint( get_post_meta($post->ID, 'Facebook_Likes', true) );
    $tong_luot_likes += $luot_like;
    $luot_comment = absint( get_post_meta($post->ID, 'Facebook_Comments', true) );
    $tong_luot_comments += $luot_comment;
    $luot_plus = absint( get_post_meta($post->ID, 'Google_Plus', true) );
    $tong_google_plus += $luot_plus;

}}
// I have a taxonomy called 'tinh-trang' to filter official post or not.    
$tinhtrang = get_option('zen-id_da_duyet');
    $duocduyet = get_posts( array('author' => $user->ID, 'posts_per_page' => -1, 
                'tax_query' => array( array(
                                'taxonomy' =>   'tinh-trang',
                                'field' => 'id',
                                'terms' => array( $tinhtrang )
                                ))
    )); 
$tongduocduyet = count($tong_bai_viet);
$diem_mxh = $tong_luot_likes + $tong_luot_comments + $tong_google_plus;
$tongdiem = $tongduocduyet + $tong_luot_likes + $tong_google_plus;
$taisan = ($tongduocduyet * 500) + ($diem_mxh * 50);
$topuser[] = array( 'id' => $user->ID, 'point' => $tongdiem, 'taisan' => $taisan, 'views' => $tong_luot_xem, 'gplus' => $tong_google_plus, 'likes' => $tong_luot_likes, 'comments' => $tong_luot_comments, 'posts' => $tong_bai_viet);
//wp_reset_query();
}
$rankby = get_option('zen-rankby');
if ($rankby == 'Likes') {
// function to sort array based on Likes count
function sortLikes($a, $b) { return $b['likes'] - $a['likes']; }
usort($topuser, 'sortLikes'); // sort the array

} else if ($rankby == 'Point') {
// function to sort array based on views count
function sortPoint($a, $b) { return $b['point'] - $a['point']; }
usort($topuser, 'sortPoint'); // sort the array

} else if ($rankby == 'Views') {
// function to sort array based on views count
function sortViews($a, $b) { return $b['views'] - $a['views']; }
usort($topuser, 'sortViews'); // sort the array

} else if ($rankby == 'Comments') {
// function to sort array based on Comments count
function sortComments($a, $b) { return $b['comments'] - $a['comments']; }
usort($topuser, 'sortComments'); // sort the array

} else {
// function to sort array based on posts count
function sortPosts($a, $b) { return $b['posts'] - $a['posts']; }
usort($topuser, 'sortPosts'); // sort the array
}

foreach ($topuser as $user){
    //var_dump($user); exit;
    $rank++;
    $profile = get_userdata($user['id']);

// update data for each user
update_user_meta( $user['id'], 'user_rank', $rank );
update_user_meta( $user['id'], 'user_posts', $user['posts'] );
update_user_meta( $user['id'], 'user_views', $user['views'] );
update_user_meta( $user['id'], 'user_likes', $user['likes'] );
update_user_meta( $user['id'], 'user_comments', $user['comments'] );
update_user_meta( $user['id'], 'user_gplus', $user['gplus'] );
update_user_meta( $user['id'], 'user_tongdiem', $user['point'] );
update_user_meta( $user['id'], 'user_taisan', $user['taisan'] );
}
printf("Completed: %s queries on %s(s)", get_num_queries(), timer_stop(0));
}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.