I solved it with the following query:
For products:
INSERT INTO wp_postmeta (post_id, meta_key, meta_value) SELECT id, "_wc_pinterest_condition", "new" FROM wp_posts WHERE post_type="product" AND post_status="publish" AND NOT EXISTS (SELECT 1 FROM wp_postmeta WHERE meta_key="_wc_pinterest_condition" AND wp_posts.ID = wp_postmeta.post_id);
For variations:
INSERT INTO wp_postmeta (post_id, meta_key, meta_value) SELECT id, "_wc_pinterest_condition", "new" FROM wp_posts WHERE post_type="product_variation" AND post_status="publish" AND NOT EXISTS (SELECT 1 FROM wp_postmeta WHERE meta_key="_wc_pinterest_condition" AND wp_posts.ID = wp_postmeta.post_id);
Explaination:
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
Insert values into wp_postmeta table with the following fields: post_id, meta_key, meta_value
SELECT id, "_wc_pinterest_condition", "new" FROM wp_posts
Use the id from the wp_posts table, as well as two constants (“_wc_pinterest_condition” for meta_key and “new” for meta_value)
WHERE post_type="product" AND post_status="publish"
only for products (not the variations in this example) and the ones, that are published (not trashed, draft, etc)
AND NOT EXISTS (SELECT 1 FROM wp_postmeta WHERE meta_key="_wc_pinterest_condition" AND wp_posts.ID = wp_postmeta.post_id);
and where there is not already a condition set for this product (would result in a double key)
Probably there are “better” ways like replace or on duplicate, but this also works ??
Hope that helps.
FYI I did something similar with Google Product categories:
- Exported wp_posts for published products and variations (id, post_title)
- Filled out in Excel Spreadsheet all the Google Categories
- Removed post_title column (was just for easy editing), Exported that into a text file
- Regex Replace to create statements “INSERT INTO …”
- Exectuted those thousands of queries