0

This is my code:

$user_user_count = get_user_meta( "_user_count", $user_id );
$user_user_count =  ( isset( $user_user_count ) && is_numeric( $user_user_count ) ) ? $user_user_count : 0;
update_user_meta( $user_id, "_user_count", ++$user_user_count );

When I do the action, it adds the meta successfully with 1. The issue when trying to do the action again is that: it's not changed to 2, it's still 1.

1 Answer 1

3

The problem here is twofold:

  • the ++ operator is applied on an array.
  • the user id and the meta key need to be swapped in get_user_meta()

So use get_user_meta( $user_id, "_user_count", true ); to get user meta.

Note that the third input parameter is false by default and an array is returned. Setting it to true, returns a single value instead.

Your Answer

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

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