Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
Nice answer, and welcome to the site. A warning, though... the guid field is not guaranteed to be the image url. If you move your site, or change the domain name, the guid won't change and will still be the url of the image when it was first uploaded, not the actual url (which is stored in postmeta). If you can skip that step somehow (start from the image ID rather than the url) your code will be much more stable. –  goldenapples Feb 3 '12 at 22:41

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.

share|improve this answer
1  
    
It sounds like it would be possible using the Media Tags plugin, he just replied to a query. The plugin author said "Within your own callback function for example the first parameter passed into the function is the image post array. From this you could grab the post_parent field and show the link to the parent (remember each image can be associated with only one parent)." –  Stephen S. Feb 2 '12 at 18:13
    
Figured it out and edited my original question. –  Stephen S. Feb 2 '12 at 21:25
    
You should post that solution as an answer, and then accept it. :) –  Chip Bennett Feb 2 '12 at 21:54
    
I tried, but I don't have enough credibility yet. Will do in a few more hours when the time period is expired. –  Stephen S. Feb 2 '12 at 22:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.