The reason why in the code, you show there, that delete_post_meta()
is run first, is because add_post_meta()
is run after it. If the delete wasn’t done first then you would end up with multiple entries stored in the meta table.
To be honest it would be better to use update_post_meta()
rather than both delete_post_meta()
and add_post_meta()
. The reason for this is that update_post_meta()
will try to update the existing value and if it doesn’t exist; will add the value.
Related Posts:
- Why WordPress choose data serialization over json_encode?
- Dealing with Many Meta Values, 30+
- What is the most secure way to store post meta data in WP?
- How to delete outdated, wrongly sized images in _wp_attachment_metadata?
- Meta query interfering with orderby relevance
- Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
- How to solve slow WordPress site caused by attachment_metadata
- Delete all post meta except featured image
- Is it possible to store visitors IPs in wp_postmeta table?
- Multiple postmeta with same name for one post in wp_postmeta table
- Delete all post meta except featured image Using SQL
- One post carries 30 postmeta values, is this too much?
- WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
- Storing data for a wordpress plugin
- Where does the actual data stored by add_post_meta
- Structure of postmeta meta_value for woocommerce product download
- Routine to convert custom post meta from old to new value
- Saving zero as meta value
- Uknown meta entries in wp_postmeta
- Does post_meta data need to be unserialzed?
- Link image with data from the wpdb
- Custom Post Type not showing anything added by code anymore?
- Compare User meta_value with Product meta_value
- How to save EXIF metadata in WordPress database?
- _wp_attachment_metadata is not being added to database when PDF files uploaded
- “BS_” rows in postmeta table
- Migrating meta_key and meta_value from old theme to new one
- Database structure for thousands of posts
- How much post meta data is too much?
- How do i get value from wp_postmeta?
- Recommended Post Structure for DB Storage
- SQL query : Select a thumbnail for a media
- What is the difference between an ORM and an ODM?
- Rename a table in MySQL
- How should I tackle –secure-file-priv in MySQL?
- Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
- Keeping WP database synced across multiple developers using git
- Post meta vs separate database tables
- Is it possible to define two databases for one installation?
- Connect forms in WP to external database
- Hook into $wpdb
- How to get site URL if database is disconnected?
- Is it wise to delete post revisions and autosaves from database?
- Improve or optimize a very slow query
- Using two different DB users on one WP install
- Usage of Direct Database Call Without Caching Error
- Can I transfer a mysql database to another site?
- MySQL Syntax Error upon restoring database from backup [closed]
- Weird issue with database migration and SSL links
- Merge two databases: OLD SITE -> NEW SITE
- Backup another database with BackUpWordPress plugin [closed]
- Easiest Way to Authenticate WP Users Using an External DB?
- How to use a different database to list and manage comments in the backend
- How to check if a core update, like 4.9.8 → 5.0, involves a database update (migration)?
- Is there a way to see history of WordPress database changes?
- Exporting table to csv works in the admin but exports HTML data when used on the front end
- wp search-replace (not executing)
- How to have an external DB read and get data from my wordpress database
- Import users from old WP site to new one
- SSO system between 2 WordPress installs on different servers
- is $wpdb->get_results() safe enough
- Can’t update WordFence Options, clear data manually
- Is it possible to have more than 1 MySQL database in my WordPress blog?
- How to fetch records from database WordPress
- Best practice to import user base (subscribers) from one website to another?
- AJAX wp-mysql running too slow
- Some weird users in database
- Delete/Replace words from X to Y in post content
- Location of image metadata on my server
- How to create more than one new wpdb object?
- Multiple WordPress sites installs sharing the same database but with different content and domains
- Remote database -> massive response time increase?
- Is it good practice to import/export a blog to test a redesign?
- Getting value from database table depending on field value
- Getting “Error Establishing a Database Connection” on localhost in new Mac OSX Lion environment
- Secure way to use name_save_pre?
- wpdb->insert not inserting first variable
- Database tables for sports betting integrated in WordPress
- Storing transients giving database error
- I want to replicate a wordpress website over multiple servers
- Is there anyway to split posts to different databases?
- wp-cli + valet: “Error establishing a database connection”
- local wordpress broke after changing URL
- Unable to login to a https website via the app after changing from http
- mysqli_query(): MySQL server has gone away for Curl Request
- WP website showing blank page after moving from subdomain to main domain
- create a table using user meta custom data
- WPCLI search and replace in a particlar site dir effect another site-dir
- How do I find out from my database what version of WordPress I need?
- How to create a database table?
- Hang Up Followed By Can’t select database
- Database error after importing WordPress
- Pulling values from a sepcific row in table
- import (migration) user database to wp-users
- Create hundreds of users with just ID in phpMyAdmin
- What is best for saving lot of extra detail of user?
- Insert Extra fields added in the front end registration form to DB
- wp_usermeta key problem
- Database Queries are crashing the server
- Uploading to WordPress Database