5

I have a list of attachment IDs which are built using this array:

$all_images = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
) );

Is it possible to take the image ID from this list and find the Title and permalink of the POST the image is attached to?

I know it's feasible because the Media Library shows it, but I can't find the right way to do this with the codex.

I have tried this code, however it returns the title and permalink to the attachment itself, not the post it's attached to:

$parent = get_post_field( 'post_parent', $imgID);
$link = get_permalink($parent);
1
  • 1
    Googlers: $attachment->post_parent is the ID of the post that the attachment is attached to.
    – rinogo
    Jun 20, 2017 at 23:07

3 Answers 3

6

So, if you start with this:

$all_images = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
) );

Then $all_images is an array of objects. Step through each one:

foreach ( $all_images as $image ) {}

Inside that foreach, you can use the normal parameters available to the $post object:

  • $image->ID is the ID of the attachment post
  • $image->post_parent is the ID of the attachment post's parent post

So, let's use that, to get what you're after, using get_the_title() and get_permalink():

// Get the parent post ID
$parent_id = $image->post_parent;
// Get the parent post Title
$parent_title = get_the_title( $parent_id );
// Get the parent post permalink
$parent_permalink = get_permalink( $parent_id );

That's pretty much it!

Putting it all together:

<?php
// Get all image attachments
$all_images = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
) );
// Step through all image attachments
foreach ( $all_images as $image ) {
    // Get the parent post ID
    $parent_id = $image->post_parent;
    // Get the parent post Title
    $parent_title = get_the_title( $parent_id );
    // Get the parent post permalink
    $parent_permalink = get_permalink( $parent_id );
}
?>
1
  • I guess I was over thinking it. This worked perfectly!
    – Pat
    Mar 1, 2012 at 16:21
2

The $images, is an array of post objects (attachments). You can use wp_list_pluck to extract their parent's ID into an array. (array_unique and array_filter remove duplicate IDs and empty IDs respectively - this may /may not be desirable).

You can them loop through the IDs and use get_permalink and get_the_title to obtain the post's permalink and title:

$images = get_posts( array(
    'post_type' => 'attachment',
    'numberposts' => -1,
) );
$parents = array_filter(wp_list_pluck($images,'post_parent'));
$parents = array_unique($parents);
echo "<ul>";
foreach ($parents as $id){
    echo "<li><a href='".get_permalink($id)."' >".get_the_title($id)."</a></li>";
}
echo "</ul>";
-1

We can use simply https://wordpress.org/plugins/find-posts-using-attachment/

I hope it is the best way!

2
  • Explaining what the plugin does and how it answers the question would improve your answer.
    – Gabriel
    Jun 27, 2016 at 1:10
  • This plugin adds a new column on media directory tables and all of posts that using an attachment was shown in it.
    – magic-boy
    Jul 1, 2016 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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