0

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 
WHERE  ID IS IN
       (SELECT post_id 
        FROM wp_posts
        WHERE post_content LIKE '%Customer Reviews%')
       AND post_type = 'post';

Anyone know how to do this please?

6
  • 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). Commented Jul 1, 2013 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: Commented Jul 1, 2013 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. Commented Jul 1, 2013 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. Commented Jul 1, 2013 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. Commented Jul 1, 2013 at 17:28

1 Answer 1

1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.