WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to select all wordpress posts that have a certain text string.

Then to add a custom field to all those posts.

Here is what I figured out so far but it doesn't work

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 
       'customer_reviews' AS meta_key, 
       'On_or_delete' AS meta_value
FROM   wp_posts 
       (SELECT post_id 
        FROM wp_posts
        WHERE post_content LIKE '%Customer Reviews%')
       AND post_type = 'post';

Anyone know how to do this please?

share|improve this question
If you use PHPMyAdmin you can easily create the MySQL query you need to select all posts where post_content like 'something' and do a PHP loop to add the custom fields. If this is the only issue, the Question is off-topic (pure PHP and/or pure MySQL). – brasofilo Jul 1 '13 at 15:34
What a vague answer. Your vagueness is strange in the case of answering a technical question that specifically states what the intended solution is to be. I've said I know how to do one part, and I've actually got further: – Mark Jul 1 '13 at 16:45
It's not an Answer. The Comments area of a Question&Answer site is used to provide feedback and request clarifications about the Q, so it can be improved. Check the pages tour and help center. – brasofilo Jul 1 '13 at 16:51
Apologies. I have updated the question with more information and a clearer picture of what I've tried to do. I'm new to this so you have to forgive my misunderstanding of both SQL and WP. Thanks. – Mark Jul 1 '13 at 17:07
The WP way of doing this is with a custom plugin with one DB query (get post ID's where content LIKE) and a loop with a simple update_post_meta(). If your Question is simply how to correct this SQL query, I believe it's off-topic here, it's being close-voted as such, and candidate to migration to Stack Overflow. – brasofilo Jul 1 '13 at 17:28
up vote 1 down vote accepted

Here you go (you're really close):

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id,
'customer_reviews' AS meta_key, 
'On_or_delete' AS meta_value
FROM wp_posts WHERE wp_posts.post_content LIKE '%Customer Reviews%'
   AND wp_posts.post_type = 'post';


Edit: You could also just change IS IN to IN. I rewrote the from part of the query since a subquery was unnecessary though.

share|improve this answer
Thank you very much. I see I was very close. Awesome. – Mark Jul 1 '13 at 19:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.