From ed6fa5c090da0e7a4f574e8d69e9ac5c8ee6ba62 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 6 Dec 2022 19:22:24 +0000 Subject: [PATCH] Media: Use featured image if available for attachment as preview instead of icon. When rendering a list of attachments in `WP_Media_List_Table` class, none image attachments, show a generic icon. However, attachment types like audio and video support adding a featured image. For attachments that have featured images, us this image instead of the icon. This featured image is a better preview than a generic icon. Props spacedmonkey, samful, johnbillion, JavierCasares, seanchayes, antpb, cadic, JeffPaul. Fixes #49852. git-svn-id: https://develop.svn.wordpress.org/trunk@54941 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-media-list-table.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/includes/class-wp-media-list-table.php b/src/wp-admin/includes/class-wp-media-list-table.php index 09c98a4837..0330b67f3b 100644 --- a/src/wp-admin/includes/class-wp-media-list-table.php +++ b/src/wp-admin/includes/class-wp-media-list-table.php @@ -110,8 +110,10 @@ class WP_Media_List_Table extends WP_List_Table { 'per_page' => $wp_query->query_vars['posts_per_page'], ) ); - - update_post_parent_caches( $wp_query->posts ); + if ( $wp_query->posts ) { + update_post_thumbnail_cache( $wp_query ); + update_post_parent_caches( $wp_query->posts ); + } } /** @@ -424,8 +426,18 @@ class WP_Media_List_Table extends WP_List_Table { public function column_title( $post ) { list( $mime ) = explode( '/', $post->post_mime_type ); + $attachment_id = $post->ID; + + if ( has_post_thumbnail( $post ) ) { + $thumbnail_id = get_post_thumbnail_id( $post ); + + if ( ! empty( $thumbnail_id ) ) { + $attachment_id = $thumbnail_id; + } + } + $title = _draft_or_post_title(); - $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true, array( 'alt' => '' ) ); + $thumb = wp_get_attachment_image( $attachment_id, array( 60, 60 ), true, array( 'alt' => '' ) ); $link_start = ''; $link_end = '';