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() ) {
$the_query->the_post();
$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);
}
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() ) {
$the_query->the_post();
$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);
}
Share
Improve this question
asked Sep 8, 2019 at 20:51
srle89srle89
1
1 Answer
Reset to default 0You 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745174835a4615138.html
评论列表(0条)