I am doing a migration from a wordpress/woocommerce website, I've successfully export/import all the data except one thing : the download attachment file from my woocommerce product

I'm using the wp-cli it's working fine for the normal media (all my page and blog article have their media files) but all my woocommerce files are uploaded in wp-content/uploads/year/month folder instead of : /wp-content/uploads/woocommerce_uploads/year/month

I've looked in the XML the path is correct

Is someone know how wordpress decides in which directory it will put the files during import ? How can i upload my woocommerce download product in the right directory ?

Edit : export from wp 4.8.6 & wc 3.2.1 import to wp 4.9.5 & wc 3.3.4

1 Answer 1


Here is the solution i've found : in wordpress-importer plugin function process_posts :

// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
// e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
$postdata['upload_date'] = $post['post_date'];
if ( isset( $post['postmeta'] ) ) {
    foreach( $post['postmeta'] as $meta ) {
        if ( $meta['key'] == '_wp_attached_file' ) {
            if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches) )
                $postdata['upload_date'] = $matches[0];
                $postdata['woocommerce'] = false;
            else if ( preg_match( '%^(woocommerce_uploads)/([0-9]{4}/[0-9]{2})%', $meta['value'], $matches ) ) {
                $postdata['upload_date'] = $matches[2];
                $postdata['woocommerce'] = true;

(this is the part where wordpress try to grab the upload date using the name of the directory - with woocommerce_uploads structute it will failed)

in wordpress-importer plugin function fetch_remote_file :

function fetch_remote_file( $url, $post ) {
        // extract the file name and extension from the url
        $file_name = basename( $url );

        global $wp_filter;
        if ( isset($wp_filter['pre_option_upload_path']) ) {
            remove_filter( 'pre_option_upload_path', 'woocommerce_path_upload' );

        if ( $post['woocommerce'] == true ) {
            add_filter( 'pre_option_upload_path', 'woocommerce_path_upload' );

        $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );

(this is the part before uploading file we add a path before the upload path if it's woocommerce file)

function woocommerce_path_upload () { return 'wp-content/uploads/woocommerce_uploads'; }

(the function uses in filter to correct the path)

And finally in wordpress core wp-includes/functions.php set $refresh_cache to true (default is false) :

function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = true ) {

(if you don't modify that wordpress keep in cache the upload path and don't check it on each file in our situation we need to check it everytime)

pay attention this is modification you do only during the migration after that reverse it

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.