Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Batch adding custom field to all posts in WordPress

Stanislav KhromovStanislav Khromov

The MySQL query below will add my-custom-field with value “hello” to all published posts.

INSERT INTO wp_postmeta (post_id, meta_key, meta_value) SELECT wp_posts.ID, 'my-custom-field', 0 FROM wp_posts WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type='hello';

Note
Fields that already have custom field my-custom-field will get another one. (So they will have 2 in total.)

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 1
  • Philipp
    Posted on

    Philipp Philipp

    Reply Author

    The query is doing something else:

    It inserts the custom field “my-custom-field” with value “0” to all custom posts with the post type “hello”.

    The correct query for the description would be:

    `
    INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
    SELECT wp_posts.ID, ‘my-custom-field’, ‘hello’
    FROM wp_posts
    WHERE wp_posts.post_status=’publish’ AND wp_posts.post_type=’post’;
    `