Update or insert meta key value - On Duplicate key update

I need to bulk update some meta descriptions and have found that some of the posts don't have them so I need to do an update or insert query.

(Once I get this query right I will be creating a temp table and insert using a CSV)

I have so far got to the following:

INSERT INTO wp_postmeta(post_id,meta_key,meta_value )
VALUES ('1117','_yoast_wpseo_metadesc', 'Test 5')
ON DUPLICATE KEY UPDATE post_id = 1117, meta_key='_yoast_wpseo_metadesc'

But this just inserts a new row.

So if a row exists with the post_id and the meta_key='_yoast_wpseo_metadesc' I need to update this row.

If a row doesn't have meta_key='_yoast_wpseo_metadesc' I need to insert a new row witht the post_id, meta_key and meta_value.

Any help appreciated

Topic duplicates mysql Wordpress

Category Web


Use WP's update_post_meta() function instead of straight SQL. If the postmeta exists, it gets updated. If it doesn't exist, it gets added.

update_post_meta('1117', '_yoast_wpseo_metadesc', 'Test 5');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.