admin管理员组文章数量:1415476
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.
本文标签: Update usermeta field from postmeta custom field
版权声明:本文标题:Update user_meta field from post_meta custom field 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745174835a2646185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论