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

Delete custom post type metadata without deleting the post in admin area

You have add_post_meta() and update_post_meta(), but you only need the latter:

This may be used in place of add_post_meta() function. The first thing
this function will do is make sure that $meta_key already exists on
$post_id. If it does not, add_post_meta($post_id, $meta_key,
$meta_value)
is called instead and its result is returned.

What you are doing now with both functions back to back is pointless and is causing double writes to the database.

Second, assuming that the rest of your code works, update_post_meta() itself should overwrite any previous data for the post ID and key in question. You don’t need to explicitly delete the value. The only time you’d need more complicated logic would be if you wanted to delete data but not replace it, and that doesn’t appear to be what you are trying to do (though I could be wrong). I’d add an explicit “delete data” checkbox to the form for that case, then use delete_post_meta().

Related Posts:

  1. Detect meta value changes when post is updated (post_updated)
  2. Displaying Meta Box Image
  3. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  4. Populate Custom Fields in a Custom Post Type?
  5. How to check if user meta field is empty in conditional else statement
  6. A better way to add a meta box to custom post types
  7. create custom meta box with default value
  8. How can I output WPAlchemy repeating fields meta values in my page template?
  9. Add a class to post_class if more than one post shares same meta_value_num
  10. Add a meta field to the list of results for a custom post type
  11. Empty meta-box returns publishdate if no value is set?
  12. Custom Post Type meta data getting deleted on bulk editing taxonomies
  13. what is the correct way to compare dates in a WP query_posts meta_query
  14. Advanced search form with filters for custom taxonomies and custom fields
  15. Help Creating a Slideshow Custom Post Type with Custom Meta Boxes?
  16. Correctly delete posts with meta and attachments [duplicate]
  17. Custom field values deleted when trashing custom post type
  18. How to get all custom fields of any post type
  19. Admin Area Custom Type Search By Meta Fields Without Title & Content
  20. How do I Paginate Search Results for Custom Post Types?
  21. Custom Post Type – Taxonomy Dropdown Menu?
  22. How can I add a column/s to wp_posts table?
  23. echo value from ‘select’ field type into page template using cmb2?
  24. How do I remove all the metaboxes for a custom post type?
  25. How Can I save multiple records in same meta key?
  26. wp_generate_attachment_metadata gives me an empty array
  27. Add custom field to media attachment image attribute in post editor
  28. How to get custom metabox image field?
  29. Get all posts from custom post type and insert in select input as metabox
  30. Search everything (posts, pages, tags, cpt, meta)
  31. Custom field being erased after autosave
  32. Order custom posts by a date metabox
  33. How to select one major category (or custom taxonomy) for a custom post type?
  34. How to sort CPT by custom meta value (date), and return posts month by month
  35. Use Custom Post Type as Custom Field
  36. Saving multiple Metabox contents
  37. Using ‘strtotime’ function to convert a custom-meta-box to a date-stamp
  38. How to get source of custom meta image?
  39. Metabox saving values
  40. Having trouble with custom post type / meta box
  41. add_meta_box: Datepicker like the one for postdate?
  42. How can I include meta box content when searching?
  43. Stopping WordPress from Auto Generating Image Files for Sizes
  44. update a post meta from a single table cell TablePress
  45. update custom post type meta from a shortcode
  46. Custom Meta Boxes – Nonce Issue – Move to trash issue
  47. Multiple information using custom post type
  48. How to keep a check box in custom meta box for custom post type checked by default for add new post?
  49. How to add multiple featured image in meta box in post editor?
  50. Show metabox in custom-post-template depending on taxonomy term?
  51. how to set default value for checkbox in wordpress
  52. ACF Upload Image in repeater from front-end with custom form? – add_post_meta()
  53. Displaying custom posts only if custom meta box’s date is not expired
  54. Display custom post on home page based on a metabox selection
  55. Displaying Metabox value (custom post type taxonomy)
  56. How can I display my custom metaboxes on a custom post template?
  57. Meta Box Only in CPT Edit Page, Not in Add New
  58. get_post_meta is returning image id
  59. Create a random unique 6 digit number as custom field for custom post type
  60. Why do I lose the content of meta boxes when I leave the page?
  61. Dynamically add / duplicate custom meta in custom post types
  62. how to interconnect custom post types?
  63. WordPress custom meta field for custom post not storing data
  64. How to Duplicate (multiple meta box)?
  65. window.send_to_editor and jQuery .attr() conflicts with multiple custom upload image meta boxes
  66. List of Posts in a Custom Field
  67. Save an array from drop-down in custom meta box
  68. List all images from a single post meta value
  69. Adding dropdown select meta box to custom post type – seems restAPI is interfering
  70. Custom meta box data array: foreach not working correctly?
  71. Undefined Variable – Custom Post Type Meta
  72. Keep display metadata value on backend – Custom Metabox
  73. Populate dropdown from one custom post type inside another custom post type
  74. Meta Key Value in current-user-only loop
  75. Why is my select meta data not saving?
  76. dynamically add a custom field or metabox to custom post type [duplicate]
  77. How to implement a Google map store locator
  78. How can I get the number of custom post type posts that have a specific attachment image set?
  79. Posting to a Custom Post Type from front end – user generated content
  80. Visual editor issue by having multiple tiny mce editors in a CPT
  81. Update Post Meta for a logged in user
  82. Found 2 elements with non-unique id (#_ajax_nonce) and (#_wpnonce)
  83. Plugin – Combine Meta Box Input Fields into single saveble record
  84. Two Custom Post Types Many to Many Relationship
  85. get_post_meta not working on publishing
  86. Meta Query Filtering not working on Custom Meta Box using Radio Buttons
  87. How do I set all of a particular post meta to a value within the custom post type I’m in?
  88. Grouping metadatas into one
  89. Metabox not show in categories custom post type cmb2
  90. Repeatable Fields Metabox with Textarea (or wp_editor)
  91. How to use TinyMCE Editor for one of my custom post meta field?
  92. Hierarchy and access control for Custom Post Types (CPT)
  93. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  94. Show Custom Post Type meta boxes only on Page Edit
  95. Cannot save CPT meta box
  96. Adding Page Templates to post but it ignored it
  97. Importing Data from a Non-WordPress database, into WP
  98. Permalinks: custom structure for taxonomy – tags?
  99. Can’t publish custom post type – “You are not allowed to edit this post.”
  100. Custom fields for custom post type
Categories custom-post-types Tags attachments, custom-field, custom-post-types, metabox, post-meta
Checking Taxonomy Terms for a Custom Post Type With get_the_terms Not Working
WordPress giving error when I log in after trying emergency.php

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