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,
is called instead and its result is returned.
$meta_value)
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:
- Detect meta value changes when post is updated (post_updated)
- Displaying Meta Box Image
- Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
- Populate Custom Fields in a Custom Post Type?
- How to check if user meta field is empty in conditional else statement
- A better way to add a meta box to custom post types
- create custom meta box with default value
- How can I output WPAlchemy repeating fields meta values in my page template?
- Add a class to post_class if more than one post shares same meta_value_num
- Add a meta field to the list of results for a custom post type
- Empty meta-box returns publishdate if no value is set?
- Custom Post Type meta data getting deleted on bulk editing taxonomies
- what is the correct way to compare dates in a WP query_posts meta_query
- Admin Area Custom Type Search By Meta Fields Without Title & Content
- echo value from ‘select’ field type into page template using cmb2?
- wp_generate_attachment_metadata gives me an empty array
- How to get custom metabox image field?
- Search everything (posts, pages, tags, cpt, meta)
- Custom field being erased after autosave
- Order custom posts by a date metabox
- How to select one major category (or custom taxonomy) for a custom post type?
- How to sort CPT by custom meta value (date), and return posts month by month
- How to get source of custom meta image?
- How can I include meta box content when searching?
- Stopping WordPress from Auto Generating Image Files for Sizes
- update a post meta from a single table cell TablePress
- update custom post type meta from a shortcode
- Custom Meta Boxes – Nonce Issue – Move to trash issue
- Displaying custom posts only if custom meta box’s date is not expired
- Display custom post on home page based on a metabox selection
- Displaying Metabox value (custom post type taxonomy)
- How can I display my custom metaboxes on a custom post template?
- get_post_meta is returning image id
- Create a random unique 6 digit number as custom field for custom post type
- Why do I lose the content of meta boxes when I leave the page?
- how to interconnect custom post types?
- Adding dropdown select meta box to custom post type – seems restAPI is interfering
- Keep display metadata value on backend – Custom Metabox
- Populate dropdown from one custom post type inside another custom post type
- Why is my select meta data not saving?
- How can I get the number of custom post type posts that have a specific attachment image set?
- Visual editor issue by having multiple tiny mce editors in a CPT
- Plugin – Combine Meta Box Input Fields into single saveble record
- Two Custom Post Types Many to Many Relationship
- Meta Query Filtering not working on Custom Meta Box using Radio Buttons
- How do I set all of a particular post meta to a value within the custom post type I’m in?
- Grouping metadatas into one
- Custom fields (wp_post_meta) vs Custom Table for large amount of data
- Show Custom Post Type meta boxes only on Page Edit
- Adding Page Templates to post but it ignored it
- Importing Data from a Non-WordPress database, into WP
- Can’t publish custom post type – “You are not allowed to edit this post.”
- Custom “radio button meta box” not saving correctly
- Meta Data for Custom Post Type not saving
- Check if post with same meta value exists
- Add post meta programmatically to attachment
- Display Repeatable Meta Box Content
- Displaying a div from an assigned meta_value when on a page
- What do the numbers mean at the end of add_action(‘save_post’)…?
- A meta box (in a custom post type) with two different type of fields
- Values from meta-box not on $_POST
- Custom post type suddenly stoped working after WordPress 5 update
- copy images from custom field to another custom field
- How to show custom field on specific custom post type posts only when filled
- Reusable metabox backend and frontend
- Cannot obtain custom meta information
- How can I modify a custom post type and custom page template for a child theme if all content seams to be handled by theme’s ‘native’ plugin?
- Save Metabox Custom Field Value
- Meta box data not saving
- Related posts meta_query CPT
- Let users upload image(s) to the post from front end
- Meta box with front-end styling
- How to retrive Custom Post Type Meta Fields in Custom WP_Query
- Cannot Save MetaBox Data in Custom Post Type
- Adding an extra parameter [string] to my posts’ permalink?
- Problem On Retrieving Post Meta Data on Custom Column [closed]
- Saving metabox keys and storing values as array
- Custom Post Type Meta Box Text Input Field Won’t Save When Blank
- Custom Meta Box with variable number of fields
- How to keep custom post type related information
- Custom attachment function not working in v5.4.2?
- How to avoid duplicate posts queried from custom meta?
- Custom Metabox Info Not Saving
- PHP Warning with Custom Fields
- Simple Data picker meta box
- How do I extract the contents of a CPT’s custom field for all posts?
- Collect custom post in a calendar
- Avoid another meta box in my custom post type
- How can I get the $key / $value pairs of custom fields that were added via 3rd party plugins or themes?
- Display a post count from a custom metabox selection
- Filter CPT based on meta box value using Flexible Posts widget?
- Advanced search form with filters for custom taxonomies and custom fields
- WP_Query arguments: Loop through custom post type – get all entries except excluded meta_key?
- Custom meta box includes
- Querying meta values within an array
- Creating entries with image attachements within posts and managing them in a list
- Post AND page parameter for WP function
- Display div based on Group metabox selection [closed]
- Display a list of posts whose meta field values are equal to the ID of the post being viewed?
- Check for custom field value in different post type than current one and do something