Post Pic

WordPress tip: Add a custom field to all posts

For some reason you sometimes need to add a custom field to all your existing posts. You can do it manually, but if you have lots of posts, the process will take a while. Here is a simple way to add a custom field to all posts quickly and easily.

Simply run the following SQL query on your WordPress database, using the command line client or PhpMyAdmin. This will automatically ad a custom field named MyCustomField to all your existing posts.
Don't forget to backup your database before using this query.

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'MyCustomField'
AS meta_key 'myvalue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'MyCustomField')
`` AND post_type = 'post';

Thanks to Abhishek for the tip!

5 Responses

Dec 08 2012 16:51

A quick remark – for future posts, it’s handy to register a metabox with the custom field so that upcoming posts have the new field by default.

Dec 19 2012 18:27

Excellent post, realy i need it ,thanks for sharing

Jan 26 2013 08:54

Nice and very handy tricks. Helpful for me. Thanks Jean.

Mar 24 2013 11:40

Have been looking for this for hours!

THANKS SO MUCH.

Apr 20 2013 10:43

This is great. Was looking for a way to display som recipe fields.
Thank you so much

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox