DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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!

Tags

6 Comments

  1. 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.

  2. Excellent post, realy i need it ,thanks for sharing

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

  4. Have been looking for this for hours!

    THANKS SO MUCH.

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

  6. I am not too clear about what is a custom field. Has it something to do with the meta-data?

Leave a Comment

Your email address will not be published. Required fields are marked *