Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Last active October 20, 2025 04:40
Show Gist options
  • Select an option

  • Save searchwpgists/2ce4efcc02d16470eaeca75d08b6b964 to your computer and use it in GitHub Desktop.

Select an option

Save searchwpgists/2ce4efcc02d16470eaeca75d08b6b964 to your computer and use it in GitHub Desktop.
Set image thumbnail for media image results on search page
<?php
// Set image thumbnail for media image results on search page
add_filter( 'post_thumbnail_html', function( $html, $post_id ) {
// Check if we're in a search context
if (
(
is_search()
|| doing_action( 'wp_ajax_searchwp_live_search' )
|| doing_action( 'wp_ajax_nopriv_searchwp_live_search' )
|| isset( $_REQUEST['swps'] )
)
&& 'attachment' === get_post_type( $post_id )
) {
$mime_type = get_post_mime_type( $post_id );
// If it’s an image attachment, use the original thumbnail
if ( strpos( $mime_type, "image" ) !== false ) {
$html = wp_get_attachment_image( $post_id, 'thumbnail' );
}
}
return $html;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment