Reputation: 4185
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.
Upvotes: 2
Views: 2912
Reputation: 143
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
var_dump(get_attached_file($post_thumbnail_id));
Upvotes: 5
Reputation: 4185
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;
}
Upvotes: 3
Reputation: 5937
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;
}
Upvotes: 3