How to change the default fallback image for Search Results
SearchWP utilizes the featured image field of posts to display thumbnail images in search results. This approach works well for standard post types that have featured images assigned, but some content types may not have featured images or may require different image handling. For these situations, you can implement custom fallback images to ensure consistent visual presentation across all search results.
Basic Fallback Image Configuration
You can use WordPress’s post_thumbnail_html hook to assign alternative images for specific post types that don’t have featured image fields or when you want to override the default thumbnail behavior. This hook provides complete control over thumbnail display and allows you to customize image presentation based on post type, content, or any other criteria you define.
For more information about this hook’s parameters and usage, refer to the WordPress developer documentation here.
Media Image Results
When working with media image results in your search, you can modify the post_thumbnail_html hook to display the original image thumbnail rather than relying on featured images. The following example demonstrates how to implement this functionality specifically for search results and SearchWP Live Ajax search contexts:
| <?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 ); |
Note: All hooks should be added to your custom SearchWP Customizations Plugin.
PDF Media Results
For PDF search results, you may want to display a placeholder image or use a custom field (like an ACF-uploaded thumbnail).
Here’s an example using a fallback image:
| <?php | |
| // Set default thumbnail for PDF results on search page | |
| add_filter( 'post_thumbnail_html', function( $html, $post_id ) { | |
| $mime_type = get_post_mime_type( $post_id ); | |
| // Target PDF media results only | |
| if ( 'attachment' === get_post_type( $post_id ) && strpos( $mime_type, "pdf" ) !== false ) { | |
| $html = "<img src='https://placehold.co/500x500' alt='PDF Thumbnail' />"; | |
| } | |
| return $html; | |
| }, 10, 2 ); |
Note: If you’re using ACF fields to assign thumbnails for PDFs, you can replace the placeholder with a dynamic field value.

