Batch update custom fields in WordPress

If most of the pages have the same custom field values and need to be updated, going through each page manually would take long time.

This can be done by running an update query directly to the database. This should be done carefully, of course.

Steps:

1) Make a backup of the database.

2) Custom fields are stored in the “wp_postmeta” table and marked with the meta keys. We need the field called “facts”, so we’ll be looking for that key.

3) Select pages that will need to have facts changed. Query:

SELECT *
FROM `wp_postmeta`
WHERE meta_key = ‘facts’
AND post_id NOT
IN ( 10, 643, 316, 532)

Note that “post_id not in” clause will exclude those posts/pages, and the custom fields won’t be updated for those pages.

4) Update query:

update `wp_postmeta` set meta_value = ‘new_meta_value_goes_here’ WHERE meta_key = ‘facts’
and post_id not in (10,643,316,532)

5) Check that everything worked fine.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s