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

  • Select * from wp_postmeta Where POST_ID='1117' and meta_key='_yoast_wpseo_metadesc' Change the post ids in the above query and store all post ids where num_rows is 0 and run insert on those and update on others. Jun 13, 2019 at 11:27

1 Answer 1


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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.