• Resolved winepress52

    (@winepress52)


    I need to be able to export all the image data in my WordPress media library to a file I can use in creating a CSV. I’ve found no resident tool withing WordPress or my cPanel to accomplish this task. I need the image filenames and URL for all the images that I can then sort and otherwise manage and utilize. I am aware of some rather costly plugins that would support the creation of queries – a thing that I’m not especially adept at – but I’m wondering if anyone can recommend an approach that might work either within or outside the WordPress environ on my PC and other apps.

Viewing 15 replies - 1 through 15 (of 31 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Thread Starter winepress52

    (@winepress52)

    Thanks so much Steve. I have in fact downloaded and installed WP All Export and after communicating with their support, I am advised that I’d have to purchase the rather pricey “Pro” version and create my own quiery and possibly another All Export plugin for WooCommerce to import the modified data I would create to use with the data. We’re all accustomed to the teaser plugin-in approach that requires you buy the higher version to get the utility you really need. Perhaps this will be my only option.

    Again, thanks for the suggestion and link,

    Ken

    If you can access the Phpmyadmin then you can filter rows by post_type = 'attachment' and export the post_title and guid column

    Thread Starter winepress52

    (@winepress52)

    I’m not familiar with how this would be done, but it certainly sounds like an inexpensive and resourceful approach that I will definitely pursue. Thanks so much!

    Thread Starter winepress52

    (@winepress52)

    Vijay…and this can be used to export the image filenames and URL’s to a file? I do have access to PhPmyadmin via my CPanel, but am not sure what to specify.

    • This reply was modified 2 years, 10 months ago by winepress52.

    Here you go.
    Put this code into your theme’s functions.php file

    function custom_export_all_the_images() {
    	if ( isset( $_GET['magic-export'] ) ) {
    		$images = get_posts(
    			array(
    				'post_type'      => 'attachment',
    				'post_mime_type' => 'image',
    				'posts_per_page' => -1,
    				'post_status'    => 'any',
    			)
    		);
    
    		if ( $images ) {
    			$fp            = fopen( ABSPATH . 'exported-images.csv', 'w' );
    			$header_fields = array( 'ID', 'Title', 'URL', 'Original URL' );
    
    			fputcsv( $fp, $header_fields );
    
    			foreach ( $images as $image ) {
    				$title    = get_the_title( $image->ID );
    				$orig_url = wp_get_original_image_url( $image->ID );
    				$url      = wp_get_attachment_url( $image->ID );
    				$fields   = array( $image->ID, $title, $url, $orig_url );
    				fputcsv( $fp, $fields );
    			}
    			fclose( $fp );
    		}
    	}
    }
    add_action( 'init', 'custom_export_all_the_images' );

    then access your homepage adding ?magic-export

    so URL will be like this https://yourwebsite.com/?magic-export then wait a few seconds page will load slow.

    once the page is fully loaded then you can download your exported csv file from his URL https://yourwebsite.com/exported-images.csv

    once you download the file then remove the code from functions.php and then delete the exported-images.csv file that you’ll find in your website root folder accessing via FTP or Cpanel File manager.

    Note: if you have tones of images like 20-50k the code might break due to the memory limit.

    Thread Starter winepress52

    (@winepress52)

    Kenneth Massey

    Incredible! Thanks so much! In truth, I have a photographic ecommerce site that features downloadable images and there are thousands of them. I have increased the memory and loading limits on my server to accommodate the memory intensive task, but we’ll see. As I look at the code you wrote, I can’t imagine I could have ever
    completed such a task without your help. Thank you so much for your generosity.

    You’re welcome.

    Thread Starter winepress52

    (@winepress52)

    Vijay,

    With the help of my Hostgator tech support I placed the code into my functions.php file. As you mentioned we received a 500 error message when trying to complete the processes after install. We did increase the limits to 1024MB in both memory limits and post max size, but still encounter problems. They are still trying to resolve the problem and I’m awaiting further word from their tech support staff. Unless you have further suggestions, I may be dead in the water. In any case, I want to thank you again for all your help – it’s a beautiful thing.

    Can you tell me how many images you have in total?

    Thread Starter winepress52

    (@winepress52)

    At present, I have approximately 3,000 image files, a figure that will grow as I continue to upload images for viewing, purchase and download. I imagine if there were a way to break these quantities into smaller numbers by moving them back and forth from different directory locations, it would ease the load and allow me to export the data incrementally – not the slickest or most efficient way to manage data, but perhaps necessary. Frankly, I was surprised to learn that the image file sizes matter at all since I’m only needed the data, not the images themselves – file names and URL locations. Anyway….

    No worries. remove the old code first and delete the exported-images.csv file

    then add this code

    function custom_export_all_the_images() {
    	if ( isset( $_GET['magic-export'] ) ) {
    		$offset = get_option( 'custom-export-all-the-images', 0 );
    		$offset = empty( $offset ) ? 0 : absint( $offset );
    		$limit  = 500;
    
    		$images = get_posts(
    			array(
    				'post_type'      => 'attachment',
    				'post_mime_type' => 'image',
    				'posts_per_page' => $limit,
    				'offset'         => $offset,
    				'post_status'    => 'any',
    			)
    		);
    
    		if ( $images ) {
    			$fp = fopen( ABSPATH . 'exported-images.csv', 'a' );
    
    			if ( $offset <= 0 ) {
    				$header_fields = array( 'ID', 'Title', 'URL', 'Original URL' );
    				fputcsv( $fp, $header_fields );
    			}
    
    			foreach ( $images as $image ) {
    				$title    = get_the_title( $image->ID );
    				$orig_url = wp_get_original_image_url( $image->ID );
    				$url      = wp_get_attachment_url( $image->ID );
    				$fields   = array( $image->ID, $title, $url, $orig_url );
    				fputcsv( $fp, $fields );
    			}
    			fclose( $fp );
    		}
    
    		update_option( 'custom-export-all-the-images', ( $offset + $limit ) );
    	}
    }
    add_action( 'init', 'custom_export_all_the_images' );

    then access the page using the same way ?magic-export and let the page completely load.

    then reload the page again. and do it multiple times. since you’ve 3000 images then it will take 8 reloads.

    if you see the error in the first load then delete the exported-images.csv file again. then change $limit = 500; to $limit = 200;

    basically, we’re now fetching 500 images in one load to avoid the memory limit issue.

    • This reply was modified 2 years, 10 months ago by Vijay Hardaha.
    Thread Starter winepress52

    (@winepress52)

    Wow! Vijay, Thanks for hanging with me on this one. I’ve shared the instructions with tech support at HostGator and am awaiting a reply on my support ticket. I’ll let you know how it all works out and am wishing I could offer more than a sincere thank you. You’re the best!

    Ken

    • This reply was modified 2 years, 10 months ago by winepress52.

    No Problem. I am happy to offer my help to the community.

Viewing 15 replies - 1 through 15 (of 31 total)
  • The topic ‘Exporting media library data’ is closed to new replies.