query.update_user_meta

/ Database / query.update_user_meta

 

Title : query.update_user_meta
Purpose :
  • Update user meta field based on user ID.
  • Use the prev_value parameter to differentiate between meta fields with the same key and user ID.
  • If the meta field for the user does not exist, it will be added.
Syntax :
Input Parameters :
  • user_id:
    • (int) (Required) User ID.
  • meta_key:
    • (string) (Required) Metadata key.
  • meta_value:
    • (mixed) (Required) Metadata value. Must be serializable if non-scalar.
  • prev_value:
    • (mixed) (Optional) Previous value to check before updating. If specified, only update existing metadata entries with this value. Otherwise, update all entries.
    • Default value: ”
Return Value :  (int|bool) Meta ID if the key didn’t exist,

  • true on successful update,
  • false on failure
  • or if the value passed to the function is the same as the one that is already in the database.
Example 1 :
Code :
Output : value “Pratik MJ” will get modified at meta_key “first_name” or created newly if it executes successfully.
Description : The above example code will update user meta of meta_key “first_name”
Example 2 :
Code :
Output : value “Gladiator” will get modified at meta_key “favourite_movie” if it’s previous value matches with the “prev_value” or if the meta_key doesn’t exist it will get created newly if it executes successfully.
Description : The above example code will update user meta of meta_key “favourite_movie” if it’s previous value matches with the “prev_value”
Categories
Most Popular

Leave a Reply

Your email address will not be published.