Bulk remove a word from all WordPress URLs - sql

I've imported about 600 pages into my WordPress database and most (not all) of them have the word "park" at the end of their new URL's
domain.com/awesome-park/
I would like to bulk remove the word (and its previous dash - )change them via SQL query or other recommended method. Any advice for a safe way to change URLs inside a database would be greatly appreciated.

If you know the table and column where this url has been defined you could run next query:
UPDATE 'table_name' SET 'url_column' = REPLACE('url_column', '-page', '');

This simple plugin can do the job.
https://wordpress.org/plugins/search-and-replace/
Note : Remember you should only do this for wp_posts table and take a backup of your database before executing the query.

The slug is stored in wp_posts.post_name. So the following should work (this from the first answer above):
UPDATE wp_posts
SET post_name = REPLACE(post_name, '-park', '')
WHERE post_name REGEXP '-park$';
I do recommend backing up your WordPress database before running this query!

Related

replacing data in SQL server

I have multiple tables with corrupted data like this:
Good data</title>and some random HTML<p></p><
What I would like to do is only keep "Good data" in the cell and remove everything else. I thought I can just create a small project with EF and clean the db with C# but there might be a quicker solution with SQL only? Can you use regex or some kind of substring function in SQL?
I will manually look at the table and select the field that needs to run through the code, there is no need to automate that at this point.
UPDATE dbo.SQLInjectionVictimTableName
SET UnprotectedColumn = LEFT(UnprotectedColumn, CHARINDEX('<', UnprotectedColumn) - 1)
WHERE UnprotectedColumn LIKE '%<%';
If good data is consistently followed by </, you could use:
UPDATE YourTable
SET BadField = LEFT(Badfield,CHARINDEX('</',BadField)-1)
WHERE CHARINDEX('</',Badfield) > 0

change wordpress image directory with sql

I've changed my wordpress default upload directory from:
mysite.com/files/year/month/upload
to
mysite.com/images/upload
I'm a bit stumped on the proper sql syntax to replace /files/year/month/ with /images/.
Using phpmyadmin, I selected the correct db, selected the correct table, and searched/found what needs to be changed using this sql:
SELECT *
FROM `wp_postmeta`
WHERE `meta_value`
LIKE '%/files/%/%/%'
Now I need to REPLACE everything FROM wp_postmeta WHERE meta_value LIKE %/files/%/%/ WITH /images/
To modify the entries for the uploaded media files you need to run the queries found in this article:
http://www.dezzain.com/wordpress-tutorials/how-to-move-wordpress-uploads-path-to-subdomain/
Mainly these two queries:
UPDATE wp_posts SET post_content = REPLACE(post_content,'http://www.domain.com/wp-content/uploads','http://img1.domain.com/uploads')
and:
UPDATE wp_posts SET guid = REPLACE(guid,'http://www.domain.com/wp-content/uploads','http://img1.domain.com/uploads')
The problem with the other answers is that although the upload path will be fixed for new files, the source path for media that has already been inserted into posts is still pointing to the old directory since the paths are stored in the database.
Rather than doing it that way, WordPress provides the facility to change the upload folder using the wp-config.php file.
Basically you set WP_CONTENT_DIR for the server location and WP_CONTENT_URL for the URI's to be based on.
Another option is to use the following settings:
update_option('uploads_use_yearmonth_folders', 0);
update_option('upload_path', 'images');
I'd recommend the plugin WP Migrate DB for this. That's the easy way.
If your stuck on doing it the hard way, you could either run a query using REPLACE() for each path you want to replace (the function does not allow wildcards) or try something like this.

Removing part of text from Table column

A website I am host was recently SQL injected, and I want to find a way to remove the offending injected code from a particular column (comments) in the database. Using SQL Server 2008, I'm not sure why this isn't working:
USE Dirty
SELECT REPLACE(comments,'</title><script src=http://hjfghj.com/r.php ></script>','')
FROM SALONS
You're only selecting - not updating....
Try this:
USE Dirty
UPDATE dbo.Salons
SET Comments = REPLACE(comments,'</title><script src=http://hjfghj.com/r.php ></script>','')
WHERE (possibly a condition here...)
You are not actually updating anything, merely selecting it. You need to create an update statement
USE Dirty
UPDATE SALONS
SET comments = REPLACE(comments,'</title><script src=http://hjfghj.com/r.php ></script>','')
Because the column was "ntext" that was causing an error. I managed to fix this with using a cast like this:
USE Dirty
UPDATE dbo.Salons
SET Comments = cast(replace(cast(comments as varchar(8000)),'</title><script src=http://hjfghj.com/r.php ></script>','') as ntext)

SQL find and replace text

I'm working on updating an existing wordpress database and everything is going smoothly. However, the links are still directing to the old site. Is there any way to use a loop or something to run through every record and update http://OLD_URL.com to say http://NEW_URL.com?
I might just be too lazy to manually do it but I will if it comes down to it. Thank you.
I usually run a couple of quick commands in phpmyadmin and I'm done. Here's a blog post that discusses this exact issue: http://www.barrywise.com/2009/02/global-find-and-replace-in-wordpress-using-mysql/ I would read this first: http://codex.wordpress.org/Changing_The_Site_URL to make sure all your bases are covered first.
If you want to update links in a particular table you can use the query like below:
UPDATE TableName
SET URL =
CASE
WHEN URL = 'http://OLD_URL.com'
THEN 'http://NEW_URL.com
ELSE URL
END
FROM TableName

find and replace data in multiple records

I have a mysql 5 database table with a longtext field that permits html code (via markdown) to be entered as data. Unfortunately, I made a minor copy/paste error that I didn't catch until I had more that 200 records. Because it's the same error on each record
href:"http://someurl.com"
as opposed to
href="http://someurl.com"
it would be easier if there were some sql I could write that would allow me to find "href:" on all records and replace with "href=" in the same transaction, than if I have to edit each record individually. Is there anything I can do or am I just screwed?
You can do this:
UPDATE Data_Table
SET Html_Column = REPLACE(Html_Column, 'href:', 'href=');
if using phpmyadmin click on sql and run this UPDATETable_Name
SETColumn_Name= replace(Column_Name, 'href:', 'href=')