Support

Account

Home Forums General Issues Replace Featured Image in Search Results with ACF Image?

Solving

Replace Featured Image in Search Results with ACF Image?

  • Hi Guys – I created a custom field to add a unique image to every post. I want this unique image to be displayed in the search results rather than the featured image that’s currently displayed in the search results.

    I’d like to float the image left and place it in the same position as the current featured image in the search results as well.

    Is this possible?

    P.S. I use relevanssi for search if that matters.

  • That depends on what you mean by possible.

    Do you want to edit the themes template files? or do you want to do this by adding a filter that alters what’s being output by your template files? What function is being used to display the featured image in search?

  • Hi John – Thanks for the response. I’d love to add a filter that alters what’s being output.

    Right now I am using these filters on the search.php page of my child theme.

  • I don’t know about genesis filters and actions, or how genesis displays the thumbnails. Knowing how genesis/your theme displays the images would be vital to creating a filter that alters what is output. Your first step is going to be to figure this out and I can’t really give you any advice on this.

  • Hi John – Perhaps this will help us. I really appreciate anything you can do to help.

    I’m using relavennsi and found this discussion where ACF support assisted people in getting something very similar to work. Does this info help at all?

    https://www.relevanssi.com/knowledge-base/add-custom-fields-search-excerpts/

    Using the filter supplied by Relavenssi, I entered my custom field names, but the images don’t display:

    add_filter(‘relevanssi_excerpt_content’, ‘custom_fields_to_excerpts’, 10, 3);
    function custom_fields_to_excerpts($content, $post, $query) {
    $custom_field = get_post_meta($post->ID, ‘add_image_search_results‘, true);
    $content .= ” ” . $custom_field;
    $custom_field = get_post_meta($post->ID, ‘wh1‘, true);
    $content .= ” ” . $custom_field;
    return $content;
    }

  • Some additional info: I can get my custom field titled wh1 to display in the search results, but not the image custom field named add_image_search_results.

    The wh1 field is a standard wordpress custom field, not an ACF field.

  • OK, I just got an ACF text field to display in the search results, but not an image custom field.

  • the image field stores and image id, a text field stores text. Even so, your theme is still going to output the featured image even if you find a way to add your other image field and you’ll still need to know how it is added in order to remove it.

    To use an image field in a plugin where you can specify a meta_key for that other plugin to use you must assume that the plugin will not call an ACF function to get the value and will just call get_post_meta(), meaning that you’ll only see what is stored in the database. For an image field you have to assume that this is what is returned when setting the return value for the image field to “Image ID” https://www.advancedcustomfields.com/resources/image/

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Replace Featured Image in Search Results with ACF Image?’ is closed to new replies.