change wordpress image directory with sql - 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.

Related

Bulk remove a word from all WordPress URLs

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!

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

sql mass insert. Need help

Hi I doing a mass insert of records from this text file using the script below. I having a problem here to set the "c:\" to a parameter I pass in from a vb .net application? I do not want to define a fixed path for that. IS that possible?
INSERT INTO tblContacts
SELECT *
FROM [Text;Database=C:\;HDR=Yes].[Import.csv]
The best way would be to use any scripting language (or maybe VBA) to parse the SQL file, change the path, write it again, then batch-run the changed SQL file.
Write the path as %TEMP%\Import.csv. This is a special shortcut to get to the temp folder. You will have access to write to the user's temp folder.

Remove text from within a database text field

I recently tried to import a bunch of blog posts from an old blog (SharePoint) to my current blog (WordPress). When the import completed, a lot of nasty <div> tags and other HTML made it in to the content of the post, which screwed up the way my site was rendering.
I'm able to view the offending rows in the MySQL database and want to know if there's a way to selectively remove the HTML text that may be causing problems. I could probably hack this in C# by parsing through the text, but I'd like to figure out how I can do this using SQL if I can.
If you want to see a full text sample of what one of these files looks like as it exists in the database text field, I uploaded a full sample file to my web site.
Here's want I want to do:
Remove <![CDATA[<div><b>Body:</b> from the beginning of every file
Remove the meta information at the end of every file, which might look like this:
<div><b>Category:</b> SharePoint</div>
<div><b>Published:</b> 11/12/2007 11:26 AM</div>
]]>
Remove every <div> and closing </div> tag, which might have a class attribute like:
<div class=ExternalClass6BE1B643F13346DF8EFC6E53ECF9043A>
Note: The hex string at the end of the ExternalClass can be different
I haven't used an Update statement in MySQL before and I'm at a loss for where to begin to selectively replace text within a text field. Would I use regex from within a SQL statement to help? How would I execute a statement against the remote DB?
What about cleaning up the posts before you import them? Seems like working with a local file that you can treat as a text file would be far easier. Then you could use Perl or Python to bear down on the problem to your liking before importing.
This assumes that you still have access to the data that was over in SharePoint.
There is no simple way of doing this without utilizing the back-end platform which you are using to serve your website or are most acustomed to. Myself, I would use PHP or Perl to clean the data up which will could be tricky at best. So the answer is, it can be done, but you must use some type of programming/processing language to do so, MySQL on its own won't be able to clean the data.
Assuming you are determined to use SQL like you said in your question, If you have the skill to hack it with C# you should be able to figure out how to create a stored procedure that uses a cursor in a repeat/fetch loop to select the rows, string functions to massage the data, and an update to update the row. Check this out:
http://dev.mysql.com/doc/refman/5.0/en/cursors.html

Using full-text search with PDF files in SQL Server 2005

I've got a strange problem with indexing PDF files in SQL Server 2005, and hope someone can help. My database has a table called MediaFile with the following fields - MediaFileId int identity pk, FileContent image, and FileExtension varchar(5). I've got my web application storing file contents in this table with no problems, and am able to use full-text searching on doc, xls, etc with no problems - the only file extension not working is PDF. When performing full-text searches on this table for words which I know exist inside of PDF files saved in the table, these files are not returned in the search results.
The OS is Windows Server 2003 SP2, and I've installed Adobe iFilter 6.0. Following the instructions on this blog entry, I executed the following commands:
exec sp_fulltext_service 'load_os_resources', 1;
exec sp_fulltext_service 'verify_signature', 0;
After this, I restarted the SQL Server, and verified that the iFilter for the PDF extensions is installed correctly by executing the following command:
select document_type, path from sys.fulltext_document_types where document_type = '.pdf'
This returns the following information, which looks correct:
document_type: .pdf
path: C:\Program Files\Adobe\PDF IFilter 6.0\PDFFILT.dll
Then I (re)created the index on the MediaFile table, selecting FileContent as the column to index and the FileExtension as its type. The wizard creates the index and completes successfully. To test, I'm performing a search like this:
SELECT MediaFileId, FileExtension FROM MediaFile WHERE CONTAINS(*, '"house"');
This returns DOC files which contain this term, but not any PDF files, although I know that there are definitely PDF files in the table which contain the word house.
Incidentally, I got this working once for a few minutes, where the search above returned the correct PDF files, but then it just stopped working again for no apparent reason.
Any ideas as to what could be stopping SQL Server 2005 from indexing PDF's, even though Adobe iFilter is installed and appears to be loaded?
Thanks Ivan. Managed to eventually get this working by starting everything from scratch. It seems like the order in which things are done makes a big difference, and the advice given on the linked blog to to turn off the 'load_os_resources' setting after loading the iFilter probably isn't the best option, as this will cause the iFilter to not be loaded when the SQL Server is restarted.
If I recall correctly, the sequence of steps that eventually worked for me was as follows:
Ensure that the table does not have an index already (and if so, delete it)
Install Adobe iFilter
Execute the command exec sp_fulltext_service 'load_os_resources', 1;
Execute the command exec sp_fulltext_service 'verify_signature', 0;
Restart SQL Server
Verify PDF iFilter is installed
Create full-text index on table
Do full re-index
Although this did the trick, I'm quite sure I performed these steps a few times before it eventually started working properly.
I've just struggled with it for an hour, but finally got it working. I did everything you did, so just try to simplify the query (I replaced * with field name and removed double quotes on term):
SELECT MediaFileId, FileExtension FROM MediaFile WHERE CONTAINS(FileContent, 'house')
Also when you create full text index make sure you specify the language. And the last thing is maybe you can try to change the field type from Image to varbinary(MAX).