Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Insert, update or remove data from database (usermeta)

So the problem with either post meta or user meta is that if your key is e.g. _group_members_value_key you only have one item of data per user or per post in which to store the data, and you need many items for each user and many items for each post. This is two columns of data, but in the meta tables you only have one.

Let’s say you have this data:

User ID | Group Id
1 | 10
1 | 20 
1 | 30
2 | 10
2 | 40
3 | 30

To put this in e.g. usermeta for user 1 you have these choices:

user id | meta_key | value
1 | "_group_members_value_key" | "10,20,30"

Or:

user id | meta_key | value
1 | "_group_members_value_key_10" | "yes"
1 | "_group_members_value_key_10" | "yes"
1 | "_group_members_value_key_10" | "yes"

Note in the second case you have to put the group ID as part of the key.

So perhaps this second solution works for you – just add the group id to the key and use some extra logic like:

To put a user in a group:

update_user_meta($userId, "_group_members_value_key_" . $groupId, "yes")

To remove a user from a group:

update_user_meta($userId, "_group_members_value_key_" . $groupId, "no")

To check if a user is in a group:

if (get_user_meta($userId, "_group_members_value_key_" . $groupId) == "yes") {

Find all the users in a group:

$user_query = new WP_User_Query( array( 'meta_key' => "_group_members_value_key_" . $groupId, 'meta_value' => 'yes' ) );

I’m not sure if this works for what you want to do though. Let me know. These are code examples so you may have to edit them a bit for exactly what you need but this data structure should work for what you need.


It looks like $_POST['group_members_field'] will be an array, so if for example it was post ID’s you need to do something like:

foreach($_POST['group_members_field'] as $postId) {
    update_user_meta($someUserId, '_group_members_value_key'.$postId, "yes");
}

Related Posts:

  1. How can I free up the memory used by update_post_meta?
  2. WordPress delete mysql rows with string
  3. Plugin MySQL SELECT custom data and filter on user meta
  4. Should I use wpdb prepare?
  5. Post meta vs separate database tables
  6. Getting wrong relationship value in $args in wp_Query?
  7. Does using set_transient() function can lead to MySQL problems?
  8. Save metabox with multiple checkbox array
  9. How to get data from WordPress $wpdb into React Gutenberg Blocks Frontend?
  10. How is the data stored in the database?
  11. How to enable users to down-vote in this simple voting counter (that uses the post meta)?
  12. Add new user and add meta at once
  13. How to add quick edit and bulk edit fields to users admin section
  14. What is rich_editing?
  15. Passing array of strings to a SQL statement in a WordPress plugin
  16. Is there a way to list tags order by post_meta field
  17. Activation hook not creating table
  18. Is the usage of ON DELETE CASCADE wrong or not allowed on wordpress?
  19. How to edit mySQL wp_posts table from plugin php?
  20. Version upgrade: can my plugin filter the SQL statements issued during a database upgrade?
  21. WordPress admin deleted user details not removed in database. How to delete WordPress Users from Database
  22. Plugin development: get_post_meta is not working [closed]
  23. Ordering posts by metadata
  24. $wpdb -> Batch insert from XML File?
  25. query users by role
  26. Ensure function has completed before allowing another Ajax call
  27. Act on user meta updated, but only once
  28. How to check if Woocommerce Order number equals to post ID?
  29. update post meta for checkbox in the admin when inserted in the front-end
  30. 403 Error when text is pasted in Custom Metabox Textarea
  31. WordPress database error for query INSERT INTO
  32. How to connect to AWS RDS external database (not for the core WordPress db)
  33. How to add custom fields to the all users page
  34. Saving value of a selection option in comment form as comment meta
  35. Add a Save Button to Custom Meta Box [duplicate]
  36. Plugin with action ‘save_post’ needs to press publish twice on order to publish
  37. Doubts about the use of metadata and how this can affect performance on WordPress
  38. update_post_meta not working in a loop
  39. Run Shortcode of post’s custom field in functions.php / Plugin
  40. WordPress allows multiple users to have same email ID; and I’ve a problem with it!
  41. How to add user details to different tables immediately after user registration
  42. Get draggable widgets on Edit Post page
  43. mysql_real_escape_string() vs. esc_sql() in WordPress
  44. How to get all queries’s results after they have executed?
  45. How to build a fool proof AdSense revenue sharing model?
  46. How to pull user/author profile data in a plugin?
  47. How can I add a simple custom field to my plugin?
  48. $wpdb->prepare with LIKE and sprintf
  49. Alternative functions for mysql_free_result and mysql_ping in wordpress functions
  50. Associate multiple email addresses with the same user account, so they can log in with either
  51. WooCommerce change Tax Class programmatically when Recalculating an existing Order [closed]
  52. WP nonce field checkbox prints checked=’checked’ outside input field
  53. Ajax: Populate with content from a post’s ID not working – duplicating current page html instead
  54. Using AND and bracket grouping in SQL not working
  55. Change/Set Page Title and Meta Tags from Page Called within a Plugin
  56. Issues Updating Post Meta with AJAX (Seems simple but cannot figure it out)
  57. Two different wordpress sites – same server and IP address. Gaining Access to database 1 of 2
  58. Update a user profile via frontend
  59. How can I programmatically change a MySql variable (foreign_keys_check)?
  60. Using delete_post_meta for deleting multiple selected options
  61. Auto-complete or auto-suggest from stored data in database
  62. Get audio metadata on file upload
  63. What might be the reason of Couldn’t fetch mysqli_result on another domain?
  64. $wpdb->prepare is not working like mysql_real_escape_string
  65. Brainstorm – Slow Query from Plugin Need to Speed It Up
  66. Plugin can’t be activated [closed]
  67. Create an user on external database
  68. Any way to update_post_meta with html content? It gets stripped and becomes empty
  69. What could cause a WP Option to get truncated?
  70. How to relate the Category to user?
  71. Remove Meta-boxes (Yoast SEO plugin) [duplicate]
  72. Is there a way to add a link with add_post_meta?
  73. Adjust query on single
  74. WordPress database error: [Query was empty] – using $wpdb->prepare()
  75. Foreign wp_users ID in custom plugin DB table?
  76. Why is $_POST empty when saving custom Meta Box?
  77. How to Get Last ID?
  78. Catching a GET parameter from the URL and save it in the user meta when a user registers
  79. Condition to check previous next article post title
  80. register_meta not showing custom post type metabox data in rest api
  81. WordPress how do I echo SUM from a column of a MySQL table by user id AND type_operation
  82. Why is that only the first row getting inserted into Mysql table when i import csv file on backend custom plugin?
  83. Error Connecting to Database WHEN Installing WordPress on XAMPP [Tried All the Usual Stuff] (Pics Included)
  84. User meta query using Wildcard
  85. Is it possible to replace MySQL with JSON files for WordPress
  86. User avatar-ACF fields
  87. Unexpected issue when using attachment_fields_to_edit filter
  88. WordPress doesn’t create table on plugin activation
  89. Saving post meta
  90. Organising and completing posts (mark as read and hide)
  91. WordPress function is not called and ajax return 0
  92. Displaying Custom Post Meta
  93. pull data from wordpress database
  94. sql query not working in wp plugin
  95. $wpdb Mysql trigger problem
  96. Custom MySQL query to pull out Advanced Custom Fields?
  97. How to filter get_adjacent_post()?
  98. $wpdb how can i save my postmeta table before querying it
  99. Include add_post_meta in a plugin
  100. How to work with AJAX and WordPress?
Categories plugin-development Tags mysql, plugin-development, post-meta, user-meta
How to decorate WP_Widget_Text?
How to show address in one row?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress