So far I have this. What I am trying to do is to calculate total value of two fields in certain CPT. After that, I would like to update that total value field to all users. I know that need to pass all user id's to the author parameter but struggling. Possible somehow? Thanks in advance.

$users = get_users();

    $current_id = get_current_user_id();

        $args = array( 'post_type' => 'tip', 
                  'posts_per_page' => -1,
                  'author' => $current_id,
        $the_query = new WP_Query( $args ); 

        if ( $the_query->have_posts() ) {
                while ( $the_query->have_posts() ) {
                    $roi = get_post_meta(get_the_id(), 'nasnoviroi', true) ;
                    $total += (float)$roi;
    } // end while

            foreach ($users as $user) {
                update_user_meta($user->ID, 'total_roi', $total);

You cannot add something to a non-existent value; The first call would be: $total += (float)$roi; This is not possible, so Add/Initialize the variable $total = 0; before your $the_query loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.