How do I link an image to the post it is attached to?

I am using an image tagging plugin that creates galleries based on image tags, but does not link back to the original post, which is something I am hoping to do.

I found this thread: ‘Get the post attached to an image attachment‘, but it assumes I know the attachment ID.

Read More

So I guess my question is: If I have a url for an image that is attached to a post (but no attachment ID), is there any way to find out the url of the post it is attached to?


EDIT: After some additional searching I was able to come across a function to generate the attachment ID here: http://pippinsplugins.com/retrieve-attachment-id-from-image-url/:

// retrieves the attachment ID from the file URL
function pippin_get_image_id($image_url) {
    global $wpdb;
    $prefix = $wpdb->prefix;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM " . $prefix . "posts" . " WHERE guid='" . $image_url . "';"));
    return $attachment[0];  
}

and

$image_url = '{my image url variable}';
$image_id = pippin_get_image_id($image_url);

Then I was able to combine it with another WordPress Answers thread here: Get the post attached to a image attachment:

$parent = get_post_field( 'post_parent', $image_id);
$link = get_permalink($parent);

Then added:

$my_post_title = get_the_title($parent);

Then output using:

<a href="'. $link .'">'.$my_post_title.'</a>

Hopefully that makes sense to other people, but I was surprised that I was able to get it all figured out being the novice that I am.

Related posts

Leave a Reply

2 comments

  1. After some additional searching I was able to come across a function to generate the attachment ID here: http://pippinsplugins.com/retrieve-attachment-id-from-image-url/:

    // retrieves the attachment ID from the file URL
    function pippin_get_image_id($image_url) {
        global $wpdb;
        $prefix = $wpdb->prefix;
        $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM " . $prefix . "posts" . " WHERE guid='" . $image_url . "';"));
        return $attachment[0];  
    }
    

    and

    $image_url = '{my image url variable}';
    $image_id = pippin_get_image_id($image_url);
    

    Then I was able to combine it with another WordPress Answers thread here: Get the post attached to a image attachment:

    $parent = get_post_field( 'post_parent', $image_id);
    $link = get_permalink($parent);
    

    Then added:

    $my_post_title = get_the_title($parent);
    

    Then output using:

    <a href="'. $link .'">'.$my_post_title.'</a>
    

    Hopefully that makes sense to other people, but I was surprised that I was able to get it all figured out being the novice that I

  2. Try combining the_permalink() with the Attachment ID.

    For example, assuming you know, or know how to get, the Attachment ID, as $attachmentid:

    <a href="<?php the_permalink( $attachmentid ); ?>"><img /></a>
    

    However, you state:

    I found this thread: ‘Get the post attached to an image attachment’, but it assumes I know the attachment ID.

    So I guess my question is: If I have a url for an image that is attached to a post (but no attachment ID), is there any way to find out the url of the post it is attached to?

    It is implausible that you don’t already have the attachment ID. What Plugin are you using?

    Edit

    Briefly looking at the SVN code for the Plugins you referenced, I’m not seeing an obvious exposure of Attachment ID. The answer to the question, then, is probably too localized for WPSE. I would recommend posting to the Tag Gallery Plugin support forum at wordpress.org.