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.

  • 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
    Jul 22, 2021 at 18:09
  • Yeah, that would probably the most logical way to approach this situation.
    – robert0
    Jul 22, 2021 at 18:11
  • Thanks, I think I know how to do this. Will be updating my progress.
    – robert0
    Jul 22, 2021 at 18:14

1 Answer 1


Basically, 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 ); 


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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