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

Multiple postmeta with same name for one post in wp_postmeta table

wp_insert_post runs whenever a post is updated, not just when it’s first saved. So your payment_meta() function is running every time a post is saved, and since you’re using add_post_meta() in that function, a new meta entry is created every time the post is saved.

Instead of using add_post_meta() stick to just using update_post_meta() if a value isn’t already set this will still create the meta, but if it already exists it will update the existing value, rather than adding a new one:

function payment_meta($post_ID)
{
    $user_id = get_post_field( 'post_author', $post_ID );
    $user_role="editor"; 
    $post_type="portfolio"; 
    if( get_post_type( $post_ID ) == $post_type ) {
        if ( get_post_meta($post_ID, '_payment', true) != '1' && !user_can( $user_id, $user_role )) {
            update_post_meta($post_ID, '_payment', '0');
        }
        else if ( get_post_meta($post_ID, '_payment', true) == '' && user_can( $user_id, $user_role )) {
            update_post_meta($post_ID, '_payment', '1');
        }
    }
}

add_action('wp_insert_post', 'payment_meta', 11, 1);

Related Posts:

  1. “BS_” rows in postmeta table
  2. Using wpdb to connect to a separate database
  3. WPDB Insert or if exists Update
  4. get_results using wpdb
  5. Why WordPress choose data serialization over json_encode?
  6. $wpdb won’t insert NULL into table column
  7. Get error messages when $wpdb->insert() returns false?
  8. Detecting errors generated by $wpdb->get_results()
  9. Does dbDelta delete columns as well?
  10. wpdb update add current timestamp not working
  11. How to fetch Data in WordPress using MySQLi or $wpdb
  12. wpdb->insert multiple record at once
  13. WordPress Unit Testing – Cannot Create Tables
  14. How to define composite keys with dbDelta()
  15. Does wpdb add considerable overhead on queries with large result sets?
  16. Why does $wpdb return strings for mysql integer values?
  17. $wpdb->prepare() warning in WordPress 3.5
  18. So much data in postmeta
  19. Dealing with Many Meta Values, 30+
  20. Inserting data into custom tables
  21. Hook into $wpdb
  22. Display data from a non wordpress database on a page template
  23. How does $wpdb handle COUNT(*)
  24. What is the most secure way to store post meta data in WP?
  25. What is the advantage of the wp_options design pattern?
  26. How might I retrieve a featured post image from an external WP site and display it as a link back?
  27. WordPress database error: [Query was empty] [closed]
  28. Export wordpress table to excel
  29. How to close wpdb connection? [duplicate]
  30. How to delete outdated, wrongly sized images in _wp_attachment_metadata?
  31. Meta query interfering with orderby relevance
  32. Inserting Post Meta From SQL
  33. Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
  34. Why does dbDelta() not catch MysqlErrors?
  35. Connecting to external oracle database
  36. How to solve slow WordPress site caused by attachment_metadata
  37. Can’t save ajax value to database
  38. Delete all post meta except featured image
  39. What causes the “max_user_connections” warning on WordPress frontend?
  40. $wpdb variable throw this error Call to a member function get_results() on a non-object in
  41. Updating all rows of table with $wpdb
  42. Safe way to find last inserted id in a table?
  43. How to execute mulitple statement SQL queries using $wpdb->query?
  44. Is $wpdb->prepare escaping to much? How to use it properly?
  45. Database slowdown after update to 3.4.1
  46. When and why should I use $wpdb different return types?
  47. How wordpress accesses its own database
  48. Remove database entries where post_date > expiration date
  49. Error connecting to DB in /wp-includes/wp-db.php on line 1538 [closed]
  50. How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
  51. Get data from database using $WPDB
  52. How to use a different database to list and manage comments in the backend
  53. Is it possible to store visitors IPs in wp_postmeta table?
  54. Getting all the users who have author privilege
  55. Delete all post meta except featured image Using SQL
  56. WP Database Table to CSV file
  57. Cannot update custom database table row
  58. wpdb_prepare with multiple or condition
  59. One post carries 30 postmeta values, is this too much?
  60. Show last modified date of database
  61. $wpdb->insert is not working
  62. Extracting the post_id via the wp_insert_post action (external db query)
  63. WordPress database error – Error in SQL syntax – I can’t identify any error?
  64. delete post meta from db, even if does not exist?
  65. In what part of the WordPress core does the users table and usermeta table get joined?
  66. WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
  67. Storing data for a wordpress plugin
  68. Modify Database in Multi-Site wp_usermeta table
  69. Where does the actual data stored by add_post_meta
  70. Multiple wp_options tables to share content across installs
  71. Structure of postmeta meta_value for woocommerce product download
  72. Advanced SELECT query with condtional statements
  73. $wpdb doesn’t like to store arrays
  74. Select two sums with single get_var statement
  75. WordPress choose wrong database
  76. Routine to convert custom post meta from old to new value
  77. Redirecting to old domain after migration website
  78. Create table from array with prepare
  79. Update from 4.5 to 4.6 failing
  80. Does WordPress $wpdb functions wait when table is locked?
  81. How Can I Put Meta_Compare in the Database-Query?
  82. WordPress running SQL query to update database from form
  83. Saving zero as meta value
  84. Uknown meta entries in wp_postmeta
  85. $wpdb->insert Giving duplicates
  86. I’m not able to get access to $wpdb [duplicate]
  87. WPDB SQL query with prepare() returning variable, not db value
  88. $wpdb error (Call to a member function insert() on a non-object)
  89. Does post_meta data need to be unserialzed?
  90. Have working sql query… trying to adjust it to use $wpdb
  91. Is it important to have integers inserted using %d rather than %s?
  92. Problem in using wpdb
  93. $wpdb->query can’t insert data
  94. Custom WordPress Table wpdb
  95. How to fetch records from database WordPress
  96. Best practice to limit results in get_row()?
  97. Best practice to import user base (subscribers) from one website to another?
  98. AJAX wp-mysql running too slow
  99. CreateOrUpdate in WordPress
  100. Problem migrating to localhost
Categories Database Tags database, post-meta, wpdb
Private member page
Hide Author.php template from specific user role

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