DEV Community

Diego Carrasco Gubernatis
Diego Carrasco Gubernatis

Posted on • Originally published at diegocarrasco.com on

How to import several folders with files into WordPress Media Library using wp-cli

Code in terminal
I was in a pinch. I needed to import 12.000+ files with different filetypes into a Wordpress Media Library. The easy part, uploading the files with the folder structure to wp-content/uploads was done quickly, but Wordpress Media Library didn’t “see” the files.

After some quick reading I found out that they need to be imported into the Library, and after some more reading I found wp-cli works with ‘xargs’ in Bash.

Helpful links at Dev Notes and StackOverflow

The command I ended using is as follows through SSH:

find /tmp -name '*.pdf' -or -name '*.doc' -or -name '*.jpg' -or -name '*.xls' -or -name '*.docx' -or -name '*.xlsx' | xarg wp media import
Enter fullscreen mode Exit fullscreen mode

Then you can regenerate all the thumbnails using

wp media regenerate --all
Enter fullscreen mode Exit fullscreen mode

I recommend using screen as the import could stop if the connection gets down, because for me on a Digital Ocean server with 2gb took around 2-3 hours.

Source

Top comments (0)