0

I need to move all images attached to posts of a specific category (MyCat) from its current standard location (/uploads/year/month/) to a specific category folder (/uploads/mycat/).

For instance:

/uploads/2015/06/mycat-post1-image1.jpg
/uploads/2015/06/mycat-post1-image2.jpg
/uploads/2015/07/mycat-post2-image3.jpg
/uploads/2015/08/mycat-post3-image4.jpg

... and much more mycat images ...

move to...

/uploads/mycat/image1.jpg
/uploads/mycat/image2.jpg
/uploads/mycat/image3.jpg
/uploads/mycat/image4.jpg

(not necessary renaming, just for illustrating).

I need to physically move the images from source folders to target one, and also need to change the links in database. But, how to do it?

1 Answer 1

0

I had to code an external program, I can't see any other way. I used Java JDBC in my case.

Query:

SELECT p.post_title, m.meta_value
FROM wp_term_taxonomy tt
INNER JOIN wp_term_relationships tr on tt.term_taxonomy_id = tr.term_taxonomy_id
inner join wp_posts p on p.ID = tr.object_id
inner join wp_posts p2 on p.ID = p2.post_parent
inner join wp_postmeta m on p2.ID = m.post_id
where tt.term_id = 795
and p.post_status = 'publish'
and p.post_type = 'post'
and p2.post_type='attachment' and p2.post_status = 'inherit'
and m.meta_key = '_wp_attached_file'

But was not able to copy to a specific mycat, wordpress does not recognize custom folder structure for media.

Then, as all images are in current year/month, I did:

UPDATE wp_posts p
SET p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/02/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/03/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/04/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/05/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/06/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/07/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/08/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/09/', 'http://miwp.local/wp-content/uploads/2016/04/')
WHERE p.post_name LIKE 'mycat-%';

provided post_name is prefixed with 'mycat-' .

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.