• I am trying to delete unused media files. I am filtering my Media Library by Unattached files, but Featured Images are still listed as Unattached. Is there a way to search for Featured Images? Either in WordPress, or in cPanel File Manager, or in an FTP file manger? Something?

    Is there a better way to search for unused media files? I know there are plugins available, but even Media Cleaner costs money if you use a page builder. I would appreciate any help. Thanks.

    • This topic was modified 2 years, 10 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You could query the DB for all featured image IDs. Search the wp_postmeta table for meta_key = “_thumbnail_id” where the related meta_value is non-zero. You could also get the post IDs of all posts with featured images this way.

    It’s feasible make a custom script that queries for all unattached images that are not featured images and delete all related images of records so found prior to deleting the attachment records themselves.

    However, unattached and not featured does not mean unused! You can embed an image that already exists in the library in a different post. The image could still appear unattached and not featured, yet be in use. A clean up script would need to search all post content for file references as well as checking for attachment and featured status prior to deleting each candidate file and record.

    Thread Starter tjholcomb1225

    (@tjholcomb1225)

    Thanks for this explanation, @bcworkz. Can you tell me how to locate my wp_postmeta table? I can’t seem to find the info on Google.

    Moderator bcworkz

    (@bcworkz)

    You’d typically query the DB via PHP and the global $wpdb connection object. Because table prefixes (wp_ by default) can vary, the table name is stored as an object property $wpdb->postmeta. You’d query the table with mySQL via one of the $wpdb methods.
    https://developer.www.ads-software.com/reference/classes/wpdb/

    $wpdb knows where the table is so you don’t have to ?? But it’s informative to find the table and peruse it for yourself. Access it via the phpMyAdmin app, usually accessed through your hosting account. Find the correct DB that WP is using in the app’s left sidebar. It’s defined in wp-config.php if you’re not sure. Clicking the DB name expands into a list of tables. Click the wp_postmeta table (or postmeta with whatever your site’s prefix is if not wp_). The first page’s worth of data will be displayed.

    Thread Starter tjholcomb1225

    (@tjholcomb1225)

    Thanks. I feel like I’m getting closer. I found the wpdn_postmeta connection object, but noticed it was not “wpdb.” Does this still work? Here’s what I get when I look at meta_value:

    a:5:{s:5:"width";i:2448;s:6:"height";i:3264;s:4:"file";s:18:"2014/05/image1.jpg";s:5:"sizes";a:5:{s:9:"thumbnail";a:4:{s:4:"file";s:18:"image1-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:18:"image1-225x300.jpg";s:5:"width";i:225;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:12:"medium_large";a:4:{s:4:"file";s:19:"image1-768x1024.jpg";s:5:"width";i:768;s:6:"height";i:1024;s:9:"mime-type";s:10:"image/jpeg";}s:5:"large";a:4:{s:4:"file";s:19:"image1-768x1024.jpg";s:5:"width";i:768;s:6:"height";i:1024;s:9:"mime-type";s:10:"image/jpeg";}s:14:"post-thumbnail";a:4:{s:4:"file";s:20:"image1-1200x1600.jpg";s:5:"width";i:1200;s:6:"height";i:1600;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"1";s:8:"keywords";a:0:{}}}

    Does this make any sense to you? LOL. Still not quite sure how to tell if it’s attached or used anywhere.

    Moderator bcworkz

    (@bcworkz)

    That’s serialized meta data for an image. When you get or set meta data using WP functions (such as get-post_meta()), WP will unserialize of serialize the data to/from a PHP array structure for you. If you use straight SQL, use appropriate PHP functions to do so. Don’t try to directly alter serialized data. If you cause the string character counts (s:5 etc.) to be off, it’ll corrupt the remainder of the record.

    The global $wpdb connection object is used to programatically execute SQL queries. For example:

    global $wpdb;
    $sum = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) from $wpdb->posts where post_status = 'publish' and user_id = %d", get_current_user_id() ) );

    You cannot really be sure an image is not used without searching all post content and featured records for the image. Attached or not doesn’t really help.

    Thread Starter tjholcomb1225

    (@tjholcomb1225)

    Thanks, bcworkz. You have clearly delineated how far in over my head I am. LOL. I guess I’ll start working my way through each post. In the future, I will be sure to attach media files I upload. This whole thing just became a lot more tedious, and that makes Wix look really good. LOL.

    Moderator bcworkz

    (@bcworkz)

    The ability to insert any existing image into any random post is what makes the clean up process difficult. Consistently ensuring every upload is attached to a post is helpful, but for easy clean up you cannot reuse existing images in other posts. You’d need to upload anew, even if it’s an identical image. Having redundant images could make the need for clean up more likely ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Search all featured images’ is closed to new replies.