[Resolved] How to show images from attachments directly in search results?

Home Forums Support How to show images from attachments directly in search results?

  • This topic has 14 replies, 2 voices, and was last updated 1 year ago by Tom.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #998546
    Alexander

    Hello,

    on my photography site I heavily rely on attachments (obviously), and I also include media in search results. Since media doesn’t have a featured image, the photos show up in the search results as text only, which is a bit of a hindrance for visual content. 🙂

    Is there a way to include the photo itself in the search results, just as if it were a featured image for a post?

    Here’s what my search result for “Buckwheat” looks like right now: https://www.alex-kunz.com/?s=buckwheat – the first page is all attachments in this case, and it’s all text.

    Thanks a lot
    Alexander.

    #1001259
    Alexander

    *ping*

    (I guess I shouldn’t have started this topic on the holiday weekend?;-)

    #1001287
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Sorry about that! Not sure how this slipped through.

    Tough one! I wonder if this would do it?:

    add_filter( 'generate_show_excerpt', function( $show ) {
        if ( is_search() && is_attachment() ) {
            $show = false;
        }
    
        return $show;
    } );
    #1001343
    Alexander

    Hello Tom,

    no worries, no hurry – thanks for your reply!

    Your filter didn’t work but it made me look at the docs for generate_show_excerpt (I’m linking it here in case someone else needs this), and I tried the example snippet from that document to show the full post in search results – and that works.

    Best of all, it still shows my excerpts, since I have the “Read More” tag in all of my normal posts – or am I missing something here? Here’s what it looks like now: https://www.alex-kunz.com/page/2/?s=buckwheat (this result page contains both posts and attachments)

    This leads to a follow-up question now: clicking on a search result image for an attachment opens the image file itself (because it’s the attachment page, I get it). Clicking on the image for a post in the search results open the post though (because it’s a Featured Image).

    Is it possible that the images for attachments in search results (and only there) link to the attachment page instead?

    Thanks
    Alex.

    #1001913
    Tom
    Lead Developer
    Lead Developer

    Perfect!

    Let’s see if this works:

    add_filter( 'attachment_link', function( $link, $post_id ) {
        if ( is_search() ) {
            return get_permalink( $post_id );
        }
    
        return $link;
    }, 10, 2 );

    Let me know 🙂

    #1001917
    Alexander

    Thank you, Tom – unfortunately, that replaced the image with the link (I’m leaving it active so you can take a look), ie. the image itself doesn’t show anymore. Whoops!

    #1001922
    Tom
    Lead Developer
    Lead Developer

    Just made a change to the code above – can you give it another shot?

    #1001928
    Alexander

    Ooops, that resulted in a 500 Internal Server Error. :-} (I reverted it to the previous code)

    Is there an error log that would help you?

    #1002168
    Tom
    Lead Developer
    Lead Developer

    That’s strange – what’s your most recent error_log entry say? The PHP syntax looks good.

    I just tweaked it a little, but I’m not seeing why it would have errored on us: https://generatepress.com/forums/topic/how-to-show-images-attachments-in-search-results/#post-1001913

    #1002186
    Alexander

    Hello Tom, I updated the snippet with your latest tweak, it doesn’t make a difference. I deactivated the Relevanssi search plugin that I’m using, no difference either.

    Oddly, the error_log doesn’t contain anything useful – there’s one lonely entry in there, but it’s not from my IP, or the server’s IP.

    The PHP error log’s last entry is from two days ago.

    Not sure what to do. If possible, I’d like to solve this riddle, but it isn’t high on my priority list. The fact that the photos do show up in the search results is the improvement that I really wanted.

    Thank you
    Alex.

    #1002749
    Tom
    Lead Developer
    Lead Developer

    To debug, does this output “hi” on the page?:

    add_filter( 'attachment_link', function( $link, $post_id ) {
        if ( is_search() ) {
            var_dump( 'hi' );
            return get_permalink( $post_id );
        }
    
        return $link;
    }, 10, 2 );
    #1004271
    Alexander

    Hello Tom, sorry for the late reply, I’ve been out of town.

    That doesn’t make a difference. Nothing is output. The “500 Internal Server Error” is shown without any search results appearing first (ie. I search for “buckwheat” or whatever, and after a second or so, the 500 internal server error is shown).

    Thanks
    Alex.

    #1004354
    Tom
    Lead Developer
    Lead Developer

    Hmm, ok.

    I have no idea if this will work, but let’s try:

    add_filter( 'wp_get_attachment_link', function( $output, $id, $size, $permalink, $icon, $text, $attr ) {
        if ( ! is_search() ) {
            return $output;
        }
    
        $_post = get_post( $id );
    
        $image = wp_get_attachment_image( $_post->ID, 'large', $icon, $attr );
    
        return "<a href='" . get_permalink( $_post->ID ) . "'>$image</a>";
    }, 10, 7 );
    #1013860
    Alexander

    Hello Tom,

    a very late reply to this, I’m sorry! (I never received an email notification to it and thought we’d just leave it alone after my last reply:-) I saw your response only now when I posted a different question and looked at my forum profile.

    And what can I say – this last one works! The images for attachments in search pages now link to the attachment pages. Images in posts still link to the post. Fabulous!

    Thanks again & sorry that I didn’t see this any sooner.
    Alex.

    #1014077
    Tom
    Lead Developer
    Lead Developer

    No problem! Glad I could help 🙂

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