Actual file location of a WordPress featured image?

I’m generating a query for certain posts in ‘Trash’. I can retrieve the featured image URL, but I’m looking for a way to get the featured image file location on the server.

# generate the query
foreach ( $query as $post ) {
    $thumbID = get_post_thumbnail_id( $post->ID );
    $thumbObj = get_post( $thumbID );
    $source_url = $thumbObj->guid;
    # how do I get the string "/path/to/wordpress/media/library/thumbnail.jpg" ?
}

I can’t find any WordPress function that returns the actual featured image file location.

Related posts

Leave a Reply

3 comments

  1. There is no wp function but its easy enough to make your own, e.g.

    function return_image_path($thumbID) {
    
        $image= wp_get_attachment_image_src($thumbID);
        $imagepath= str_replace(get_site_url(), $_SERVER['DOCUMENT_ROOT'], $image[0]);
    
        if($imagepath) return $imagepath;
    
        return false;
    
    }
    
  2. Expanding on David’s answer, this worked for me:

    function return_image_path($thumbID) {
                        $thumbID = get_post_thumbnail_id( $post->ID );
                        $image= wp_get_attachment_image_src($thumbID);
                        $upload_dir = wp_upload_dir();
                        $base_dir = $upload_dir['basedir'];
                        $base_url = $upload_dir['baseurl'];
                        $imagepath= str_replace($base_url, $base_dir, $image[0]);
                        if ( file_exists( $imagepath) ) return $imagepath;
                        return false;
    }