Support » Plugin: Import any XML or CSV File to WordPress » Upload to folder with ID

  • Resolved MDX

    (@z0pa)


    Hello!
    Thanks for the great product!
    Tell me please
    I found this piece of code that uploads images to post date folders:

    /**
     * Upload all images for the post to a folder based on the post date (in Y/m format)
     *
     * (e.g. if the post was published on June 1st 2017, its images would
     *  be uploaded to /wp-content/uploads/2017/06)
     *
     */
     
    function wpai_set_upload_folder_by_post_date($uploads, $articleData, $current_xml_node, $import_id) {
    	if ( ! empty($articleData['post_date'])) {
    		$uploads['path'] = $uploads['basedir'] . '/' . date("Y/m", strtotime($articleData['post_date']));
    		$uploads['url'] = $uploads['baseurl'] . '/' . date("Y/m", strtotime($articleData['post_date']));
    		
    		if (!file_exists($uploads['path'])) {
    			mkdir($uploads['path'], 0755, true);
    		}
    	}
    	return $uploads;
    }
    
    add_filter('wp_all_import_images_uploads_dir', 'wpai_set_upload_folder_by_post_date', 10, 4);

    It works great! But how I can add a folder with a post ID so that the path is like this /2020/02/6532/
    Where 6532 is the post ID on this site after import.

    I tried to do so:

    function wpai_set_upload_folder_by_post_date($uploads, $articleData, $current_xml_node, $import_id) {
    	if ( ! empty($articleData['post_date'])) {
    		$uploads['path'] = $uploads['basedir'] . '/' . date("Y/m", strtotime($articleData['post_date'])) . '/' . $post_id;
    		$uploads['url'] = $uploads['baseurl'] . '/' . date("Y/m", strtotime($articleData['post_date'])) . '/' . $post_id;
    		
    		if (!file_exists($uploads['path'])) {
    			mkdir($uploads['path'], 0755, true);
    		}
    	}
    	return $uploads;
    }
    
    add_filter('wp_all_import_images_uploads_dir', 'wpai_set_upload_folder_by_post_date', 10, 4);

    But unfortunately it doesn’t work.

    Help me please.
    Thanks in advance

    • This topic was modified 2 years ago by MDX.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @z0pa

    But how I can add a folder with a post ID so that the path is like this /2020/02/6532/ Where 6532 is the post ID on this site after import.

    This is currently only possible when updating posts, at which point the post ID will be available in $articleData['ID'] (so you’d have to append that instead of $post_id).

    Thread Starter MDX

    (@z0pa)

    Very sad. What is impossible during just import.

    Plugin Author WP All Import

    (@wpallimport)

    Hey @z0pa

    Very sad. What is impossible during just import.

    Unfortunately, you’re right, it’s not possible to do this during the initial import. I’ve opened an internal task so that we can look into adding the post ID to this hook in a future release.

    I’m marking this as resolved for now, but please let me know if you still have questions about this issue. Anyone else, please open a new thread.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Upload to folder with ID’ is closed to new replies.