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

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

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #51684
    nfortiernfortier
    Participant

    I recently added featured images to posts that did not have them before. But even after re-indexing, the search results from Ajax Search Pro still show the default image instead of the featured image. An example is a search for “Pineault” for which results include “Rapport annuel 2023”, it shows our logo (default image) instead of featured image.

    #51690
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    The images are not cached, so they are fetched every single time from WordPress core. I logged in and tried a few search phrases, but I’m getting the correct images in the live results list – as far as I could tell.

    Can you link me a post where the image is incorrectly displayed in the search bar? I will check and debug that.

    #51726
    nfortiernfortier
    Participant

    Hi, I realize the response I made yesterday does not appear here so I will repeat it:

    Here are 2 examples: perform search for “Pineault”, then inspect the results. Both “Rapport annuel 2023” and “Infolettre avril 2024” (among others) do have a featured image but the search results show the default image instead (our logo). See
    https://dev.actionclimatoutaouais.org/wp-admin/post.php?post=5684&action=edit
    https://dev.actionclimatoutaouais.org/wp-admin/post.php?post=5721&action=edit

    #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.

    #51735
    nfortiernfortier
    Participant

    Thank you Ernest, very good point, I will look into that.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.