admin管理员组文章数量:1289516
Basically, I'm trying to make a point exchange between current user and author when the post meta value is updated.
As well as add +1 to post meta.
Here's what I currently have:
// Coin exchange function
//
add_action( 'updated_post_meta', 'coin_exchange_after_post_meta_update', 10, 4 );
function coin_exchange_after_post_meta_update( $meta_id, $post_id, $meta_key, $meta_value )
{
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$author_id = get_post_field( 'post_author', $post_id );
if ( 'postcoins' == $meta_key ) {
update_user_meta( $current_user_id, 'usercoins', ''); // subtract -1 from current user
update_user_meta( $author_id, 'usercoins', ''); // add +1 to post author
update_post_meta( $post_id, 'postcoins', '' ); // add +1 to post meta
}
}
The only thing that's left is to figure out how to subtract and add values.
I know there's a way using sum=0, but I'm struggling with the logic.
I would really appreciate your help.
Basically, I'm trying to make a point exchange between current user and author when the post meta value is updated.
As well as add +1 to post meta.
Here's what I currently have:
// Coin exchange function
//
add_action( 'updated_post_meta', 'coin_exchange_after_post_meta_update', 10, 4 );
function coin_exchange_after_post_meta_update( $meta_id, $post_id, $meta_key, $meta_value )
{
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$author_id = get_post_field( 'post_author', $post_id );
if ( 'postcoins' == $meta_key ) {
update_user_meta( $current_user_id, 'usercoins', ''); // subtract -1 from current user
update_user_meta( $author_id, 'usercoins', ''); // add +1 to post author
update_post_meta( $post_id, 'postcoins', '' ); // add +1 to post meta
}
}
The only thing that's left is to figure out how to subtract and add values.
I know there's a way using sum=0, but I'm struggling with the logic.
I would really appreciate your help.
Share Improve this question asked Jul 22, 2021 at 17:55 robert0robert0 2032 silver badges11 bronze badges 3- 1 Isn't this just a case of retrieving the values of those meta fields first, incrementing/decrementing the amount, then using the result in your updates? I don't believe there is any tricksy way of incrementing/decrementing meta values. – vancoder Commented Jul 22, 2021 at 18:09
- Yeah, that would probably the most logical way to approach this situation. – robert0 Commented Jul 22, 2021 at 18:11
- Thanks, I think I know how to do this. Will be updating my progress. – robert0 Commented Jul 22, 2021 at 18:14
1 Answer
Reset to default 0Basically, I have ditched the function, and now update meta data with a click on a hyperlink.
It fires this code, and does exactly what I wanted.
In case anyone interested:
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$author_id = get_post_field( 'post_author', $post_id );
$user_coins = get_user_meta( $current_user_id, 'usercoins' , true );
$author_coins = get_user_meta( $author_id, 'usercoins' , true );
$post_coins = get_post_meta( $post_id, 'postcoins', true );
$user_coins_sum = $user_coins - 1;
$author_coins_sum = $author_coins + 1;
$post_coins_sum = $post_coins + 1;
update_user_meta( $current_user_id, 'usercoins', $user_coins_sum);
update_user_meta( $author_id, 'usercoins', $author_coins_sum);
update_post_meta( $post_id, 'postcoins', $post_coins_sum );
本文标签: phpHow to add and subtract user meta values after post meta update
版权声明:本文标题:php - How to add and subtract user meta values after post meta update 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741426236a2378092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论