Wordpress database: How to remove a word from all posts - mysql

I've a word called "[stink]" in all my wordpress posts (and they're many). I would like to remove it from all at once. I've got access to mysql/phpmyadmin.. any sql command that could make this?
Thank you in advance.

You can try something like this
UPDATE wp_posts
SET post_title = REPLACE(post_title, '[stink]', ''),
post_content = REPLACE(post_content, '[stink]', '')
WHERE post_title LIKE '%[stink]%'
OR post_content LIKE '%[stink]%'
Note: Make sure that you have a solid backup before you do any changes to your database.
Here is SQLFiddle demo

Since you have access to phpMyAdmin, you can take advantage of the graphical search and replace rather than having to use SQL (which was nicely explained by peterm). From within the table (wp_posts in the case of WordPress), click the Search tab then the Find and Replace text near the top of the screen. Enter the text you wish to search for ([stink]) and what you want to replace it with (sounds like nothing), then select the table from the dropdown (you'll have to do it twice to get both post_title and post_content).
However, some WordPress instances seem to serialize their database entries. In that case, you'll need a different set of tools entirely.
As peterm notes, make sure you have a backup before making changes to your database.

Related

MySQL: Why aren't url's matching when using REPLACE?

My Situation:
I have url's in a field containing blog posts. The url's are being stored in my database with escape characters. My task at the moment is to replace some already inserted 'http' url's with 'https' url's, but REPLACE will match neither the original url nor the escaped url. I can't just replace every instance of 'http:', because I only want to affect certain links in each post, not every link.
I am very familiar with SQL, as well as REPLACE, so I'm not just asking how REPLACE works and how to use it. Another user here has tested my queries in his environment and they work. So, there must be something in my configuration that is preventing the queries from functioning as expected.
I have searched this site and google extensively for several hours and have found nothing specifically addressing my issue. Everything I have tried is included below and if there is something else I should try, I don't know what that is and I haven't found any suggestions/posts/comments that suggest doing anything differently.
Example URL:
http://test01.mysite.com
As Stored in DB:
http:\/\/test01.mysite.com
Code to Re-Create Situation:
DROP TABLE IF EXISTS test_posts;
CREATE TABLE IF NOT EXISTS test_posts (
id int NOT NULL AUTO_INCREMENT,
post_content longtext NOT NULL,
PRIMARY KEY (id)
)
INSERT INTO
test_posts
(post_content)
VALUES
('content content content Link I want to change content content content Link I don\'t want to change content content content Link I want to change content content content Link I don\'t want to change');
If I run
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http://test01.mysite.com', 'https://test01.mysite.com');
or
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com');
zero records are affected.
For testing purposes, I ran the following query which returns 0 rows.
SELECT
*
FROM
test_posts
WHERE
post_content LIKE '%http://test01.mysite.com%'
OR
post_content LIKE '%http:\/\/test01.mysite.com%'
OR
post_content LIKE '%http:\\/\\/test01.mysite.com%'
OR
post_content LIKE 'http:%/%/test01.mysite.com%';
If I run:
SELECT
*
FROM
test_posts
WHERE
post_content LIKE '%http:_/_/test01.mysite.com%'
It does return matches, but that doesn't solve the real problem of how to match when using UPDATE/REPLACE.
I have tried on two different servers and I get the same results on both.
I have tried the following Engine/Collation combinations and all return the same 0 records results:
MyISAM/latin1_swedish_ci
MyISAM/utf8mb4_unicode_ci
InnoDB/latin1_swedish_ci
InnoDB/utf8mb4_unicode_ci
Anybody know how I can write these queries so that REPLACE will find matches to those url's or what settings in my database or PhpMyAdmin may be causing the queries to return/affect 0 rows?
I think the backslash must be escaped in MySQL
field_name LIKE 'http:\\/\\/test01.mysite.com%'
Of course one could go for sure and use the single char wildcard __
field_name LIKE 'http:_/_/test01.mysite.com%'
or for your both cases: an optional backslash:
field_name LIKE 'http:%/%/test01.mysite.com%'
I'm still baffled as to why the queries with LIKE won't work, but, sadly, using those to narrow down the problem clouded my judgement and I didn't try all the same combinations in the REPLACE functions.
The following works:
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http:\\/\\/test01.mysite.com', 'https://test01.mysite.com');
If anyone can explain to me why these combinations work with REPLACE, but not with LIKE, I'd really love to know. Thanks!
There is no reason, your query won't work if you have run properly, there is something else, you may be missing here.
UPDATE
test1
SET
name_1 = REPLACE(name_1, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com')
works well and does the job of repalcing the \/ with /.
See screen-shot attached,
You may have some other problem, please check and update the question, if so.
Edit after comments
If you have more data points in URL, change query like below.
UPDATE
test1
SET
name_1 = REPLACE(name_1, '\/', '/')
Above will replace all the occurrence of \/ with /.
As \\ did not work to represent/escape a backslash, use regular expression functions:
REGEXP_LIKE('.*http:\\/\\/test01\.mysite.com.*')
REGEXP_REPLACE(field, 'http:\\/\\/', 'http://')
Here \\ should work.

Update Wordpress database, change post_content hardcoded URLs

I am not a huge expert in MYSQL nor Regex (I guess I need to use some here), I would appreciate any help.
So my problem is that I would like to change some hardcoded URLs in Wordpress post contents to different ones.
Here is an example of a URL:
http://cdn1.domain.com/wp-content/uploads/2017/05/16211742/Screenshot_051117_101304_AM.jpg
Now, 'cdn1' can be cdn, cdn1 or cdn2. We might need to run 3 queries, but that is not a problem.
I would like to change all instances of this to:
//domain.com/wp-content/uploads/2017/05/16211742/Screenshot_051117_101304_AM.jpg
So basically remove the cdn (cdn, cdn1, cdn2) part, and remove the timestamp URL part before the filename.
The Wordpress database is something like this:
UPDATE wp_posts
SET post_content =
WHERE
Thank you very much!
First of all, take a database Backup for safety.
Then you can do this with SQL Command like this
UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');
OR you can do this with the plugin called: WP Migrate DB
There has a tutorial on the plugin page. check that.
Hope it will help you. :)

Need SQL Query Help: How to Search and Replace Specific Text LIKE x AND NOT LIKE xx

and thanks in advance for any help. I'm working on fixing all broken links in a massive WordPress multisite database and need some help writing an SQL query to run via PHP MyAdmin. I've searched, but can't the perfect solution...
PROBLEM: We have more than a thousand broken links that start with http:/ instead of http://
CHALLENGE: The following would result in numerous links starting with http:///
UPDATE wp_1_posts
SET post_content = replace (post_content,
'http:/',
'http://');
PROCESS: I want to write a query to SELECT all these links first, so I can review them to ensure I don't do any damage when replacing the text string. Downloading a db dump and doing a manual S&R is not an option since we're talking about a multi-gigabyte database.
I thought something like this would work...
SELECT * FROM wp_1_posts
WHERE post_content LIKE '%http:/%'
AND WHERE post_content NOT LIKE '%http://%'
But that just throws a syntax error. Am I even close?
QUESTION #1: How can I find all instances of "http:/" without returning all "http://" instances in the query results.
QUESTION #2: How might I safely fix all instances of "http:/" without affecting any "http://" strings.
FYI: I'll admit I know just enough about this to be dangerous, and I am not familiar with regular expressions. at. all. That's why I'm turning to you for help. Thanks again!
This should work, in MYSQL:
UPDATE wp_1_posts SET post_content = replace(post_content,'http:/', 'http://')
WHERE post_content REGEXP 'http:/[^/]'

Using MySQL FULLTEXT with PHP to search for words with less than 4 characters in a shared hosting

I have a simple PHP script that does a FULLTEXT search inside a MySQL database. I am hoping there is something I can do on the PHP side to resolve this as it seems there is not much I can do on the MySQL side.
I currently have this in a PHP class, and works 100% in my localhost but fails when uploading to my shared Godaddy hosting.
$pieces = explode(" ", $searchstring);
$searchwords = '';
foreach($pieces as $value) {
$searchwords .= '+'.$value.' ';
}
$sql = "SELECT * FROM items WHERE MATCH ( ItemName, ItemDescription ) AGAINST ('$searchwords' IN BOOLEAN MODE)";
I have done a lot of research on a solution to this but haven't found any answers yet. All documentation and forums suggest to change the my.ini file ft_min_word_len=4 to for example ft_min_word_len=0 which works perfectly when testing on my localserver.
Since I need to upload my project to a godaddy shared hosting account I am unable to change the my.ini configuration file so I need a work around this error.
Example of what I am trying to achieve:
Search for 'iphone 5 LCD' the database will return all the results that have 'iphone' so it will show all 'iphone 4 home button' 'iphone 3 back' because from the original search query it skips the '5' and the 'lcd' for being less than 4 charcters. In my local copy since ft_min_word_len is set to ft_min_word_len=0 'iphone 5 lcd' only returns the items that I actually need! It works just perfect! I have attempted to fill short words like '5' with '5...' or '5___' or 'LCD_' or 'LCD*' or '5*' but it does not work. I read a solution that suggested 'padding' the short words but they didn't explain what characters to use.
Wouldn't a LIKE clause work.
Cut your search words to the desired length in php, then query WHERE (columnA LIKE '$searchword%' OR columnB LIKE '$seachword%')
or simply use php strlen to remove the words shorter than desired.

Mysql: bulk delete images from posts while still using their thumbnails

I am new in MySQL.
I would like to delete all the images that were uploaded to posts and pages of a Wordpress site, while still using them as attachments/thumbnails (as featured images).
That is why, I can’t bulk delete them from the media library, only from within posts and pages.
Since it’s a huge amount of images, I’d prefer using mysql command.
The string of the images is always written as follows - <img xxxx>, where xxx is varied based on the img location and other details such as alt, width and height etc.
The table is wp_posts and the fieldname is post_content
The closest command I came across to is –
UPDATE wp_posts SET post_content = REPLACE(post_content, '<img%>', '')
WHERE post_content LIKE '<img%>';
It doesn’t work.
Any clues?
I'm not familiar with WordPress database structures, and can't figure out exactly what you mean, but don't you just mean this:
UPDATE wp_posts SET post_content = '' WHERE post_content LIKE '<img%';
This will delete everything from the post_content column that starts with '< img'. Alternatively you can do NULL instead of ''.