Search in all posts but replace only in the published ones using phpMyAdmin
-
Hi, I’m building my site locally, and since it has hundreds of posts, I’d like to learn how to “search and replace” without making mistakes.
I already tried the best plugins for doing the job, but I noticed that they are just a more user friendly way to do it, they don’t add many new features with respect to phpMyAdmin.
So I’m learning how to do it manually via phpMyAdmin, and it seems very easy too.
In particular, I have to search for a specific string in all posts, and replace it with another string, but only in the published version of the posts, without touching the revisions.First thing I tried was the phpMyAdmin “Search and replace” built-in tool, but I found out that it replaces the text both in the published posts and in the revisions.
Moreover, I noticed that the query used by the tool is in the formUPDATE 'wp_posts' SET 'post_content' = REPLACE('post_content', 'text to replace', 'replacement text') WHERE 'post_content' LIKE '%text to replace%' COLLATE utf8mb4_bin
Then, looking at the columns of the db, I found the post_type and post_status columns, which can be useful to achieve what I want.
So in the SQL tab I run the following queryUPDATE 'wp_posts' SET 'post_content' = REPLACE('post_content', 'text to replace', 'replacement text') WHERE post_type='posts' AND post_status='publish'
which worked as expected.
But now I have a doubt, looking at the rows of the db, I found out that for a given post there is a revision “linked” to the published post. What I mean is that if we create a post and then we edit it, there will be not one but two revisions lines in the db (plus the line of the published post, so 3 lines in total), the first one is the copy of the first version of the post, and the second one is the copy of the second version of the post and it is equal to the published post.
By running the second query, the two revisions are left untouched, while the published post is edited, but I feel like also the second revision should be edited, otherwise the published post won’t be linked anymore to any revision.So my first question is, are there consequences to this? That is, is it bad to edit the published post and not to edit its linked revision?
The second and last question is about the two queries I posted (first one generated by phpMyAdim, second one written by me), is it ok not to write this part?
WHERE 'post_content' LIKE '%text to replace%' COLLATE utf8mb4_bin
I dint’t write it in the second query because I don’t see the point of the command
WHERE 'post_content' LIKE '%text to replace%'
since the first part
UPDATE 'wp_posts' SET 'post_content' = REPLACE('post_content', 'text to replace', 'replacement text')
already tells to search for the string ‘text to replace’ in the ‘post_content’, doesn’t it?p.s. yes I have a backup of the db
- This topic was modified 4 years, 7 months ago by .
- This topic was modified 4 years, 7 months ago by .
- This topic was modified 4 years, 7 months ago by .
- This topic was modified 4 years, 7 months ago by .
- This topic was modified 4 years, 7 months ago by .
- This topic was modified 4 years, 7 months ago by .
- The topic ‘Search in all posts but replace only in the published ones using phpMyAdmin’ is closed to new replies.