Batch adding custom field to all posts in WordPress

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

1 thought on “Batch adding custom field to all posts in WordPress

  1. Philipp

    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’;
    `

    Reply

Leave a Reply

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

Markdown is allowed in comments.