This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: New featured image not shown in search results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support New featured image not shown in search results Reply To: New featured image not shown in search results

#51728
Ernest MarcinkoErnest Marcinko
Keymaster

Thank you!

I found the problem – the issue is that those are not the actual document post type results, but the media attachment results, which link back to the parent document. Therefore there is no featured image for them, as media files can’t have featured images.

Fortunately there is a super easy solution for that. I have placed the following custom code to the functions.php in your theme directory:

add_filter( 'asp_results', function ( $results ) {
	// --- DO NOT CHANGE ANYTHING BELOW ---
	foreach ( $results as $k => &$r ) {
		if ( isset($r->post_mime_type) ) {
			$imx = wp_get_attachment_image_src(
				get_post_thumbnail_id($r->id), 'original', false
			);
			if ( !is_wp_error($imx) && $imx !== false && isset($imx[0]) ) {
				$r->image = $imx[0];
			}
		}
	}

	return $results;
}, 10, 1);

I can’t guarantee anything, but it should work for most cases.