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.

Is it possible to get attachment id by its name? And at once may be it's possible to get parent post, to which this attachment is assigned?

share|improve this question
2  
What have you tried so far? –  Wyck Oct 8 '13 at 5:58
    
@Wyck, just couldn't find any function to find file by its name. –  Kirix Oct 8 '13 at 6:16
    
Echoing Wyck, because Googling "wordpress get attachment id from guid" or "wordpress get attachment id from name" brings up several solutions. –  helgatheviking Oct 8 '13 at 9:04
    
@helgatheviking and did you try it or just googled? –  Kirix Oct 8 '13 at 10:28
1  
No I didn't try. My point was that Wyck asked what you'd tried and that there are plenty of functions out there for you to try. If you can tell us what you've tried and precisely what isn't working we can help you tweak that. Also, support threads such as wordpress.org/support/topic/… are marked resolved so someone must have thought the solution was valid. –  helgatheviking Oct 8 '13 at 10:45
add comment

1 Answer

You have to write custom code to get the attachment id and post_parent by name/ slug or filename(if it has not been changed during the uploading of files).

Put the below code in your theme's functions.php file

if( ! ( function_exists( 'wp_get_attachment_by_post_name' ) ) ) {
    function wp_get_attachment_by_post_name( $post_name ) {
        $args = array(
            'post_per_page' => 1,
            'post_type'     => 'attachment',
            'name'          => trim ( $post_name ),
        );
        $get_posts = new Wp_Query( $args );

        if ( $get_posts->posts[0] )
            return $get_posts->posts[0];
        else
          return false;
    }
}

then you can call the function where you need it like below :--

$attachment = wp_get_attachment_by_post_name( $post_name );
// Replace post_name by the name/slug of the attachment
// It will give you an object, which you can render like below to get the ID and post_parent
if ( $attachment ) {
    echo $attachment->ID; // Gives the id of the attachment
    echo $attachment->post_parent; // Gives the post_parent id
    echo $attachment->post_title; // Gives the attachment title.
}
share|improve this answer
    
i don't have the post name, i have only file name –  Kirix Oct 8 '13 at 8:50
    
ok, by name I thought it to be post name –  Maruti Mohanty Oct 8 '13 at 9:06
    
You can try the filename instead of the post_name as the parameter, as WordPress creates the slug for the attachment with the filename until unless it is altered. –  Maruti Mohanty Oct 8 '13 at 9:46
    
not working.... –  user1692333 Oct 8 '13 at 19:57
    
@user1692333 Will you please explain your situation where this snippet is not working? and lets make it work –  Maruti Mohanty Oct 9 '13 at 5:38
show 2 more comments

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.