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
- Using wpdb to connect to a separate database
- How can I make a WordPress database portable and url independent?
- How to stop WordPress using utf8mb4_unicode_520_ci collation?
- Exporting revisions
- WP_Options ID high
- Default WordPress settings API data sanitization
- How to use multiple database in wordpress?
- Is removing orphaned wp_postmeta records safe?
- How to intercept update_option() before it saves data?
- wpdb query on custom table not working [closed]
- Error connecting to DB in /wp-includes/wp-db.php on line 1538 [closed]
- Set Display Name to first and last name (phpmyadmin SQL Query)
- How to fix unchanged URLs in Database after running serialized search and replace script?
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- SQL Error wp_commentmeta – incorrect table definition
- WordPress database error – Error in SQL syntax – I can’t identify any error?
- Are custom tables in the database overwritten when WordPress is updated to new versions?
- What is the proper way to validate and sanitize JSON response from REST API?
- How do I get the posts within a certain year/date(with sql query)?
- batch UPDATE autoload value in wp_options table
- Displaying BLOB PDF File
- Saving into post_excerpt or post_content
- How to fix the disappearance of the “\” character when updating a post in the database
- Data storage for JS app using WP REST API
- WP CLI Core install – blank database
- How can I change the meta_key for a particular piece of information?
- Update from 4.5 to 4.6 failing
- Setup 3 Sites To Connect To 1 Database and Share Data
- How can I push local database changes to production, without impacting recent posts/pages that were published on production server?
- Does auto_load to ‘no’ in wp_options improve performance
- Remove empty rows from the database
- Custom WordPress Table wpdb
- Check if post id exist in table than only update instead of inserting new row
- Detail explanation of wordpress database fields and metatag list [closed]
- CreateOrUpdate in WordPress
- $wpdb->get_results($wpdb->prepare(… You have an error in your SQL syntax;
- Can local WordPress installs share /wp-content/ folder and database?
- how to encyrpt DB_PASSWORD in wp-config
- Choosing http or https during manual installation of WordPress and database
- Stringing together content from different databases
- Safely store code(html/js..) into database
- Why does wp_links still exist in the db schema?
- Creating and Using Tables in the WordPress Database
- can’t upgrade wordpress or install plugins, it seems to “think” it’s still on a local installation
- Sql query to get all metadata for a post
- Where do I put my create_new_table function()?
- How to import products in host instead of database on wordpress?
- syntax error for changing user role from database
- get_option() does unserialize and don’t remove \
- Want to update value in database. But it says update() on null
- Merging development site with live site
- The MySQL Connection could not be established., before it was joomla but i have change it into worpress
- Checking if the data already exsis in the wp database – custom plugin
- live site do not update after importing sql database
- Transforming an wp_options database array into a javascript obect?
- Selecting NULL value from the database
- How can I retrieve data from alternate database using wpdb class?
- Database connection close
- WordPress Database Charset/Collate
- User accounts for saving form data?
- Print output of Table Creation
- How to delete field using WPDB?
- DROP TABLE IF EXISTS `wp_options` error?
- Server database problem
- Best way to search and replace within serialized database strings?
- Have WordPress Use a Different Database for users
- WordPress post on home page of my website
- large number (bigint) for post ID issue in database