Skip to content

Instantly share code, notes, and snippets.

@ptasker
Created August 1, 2012 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptasker/3229701 to your computer and use it in GitHub Desktop.
Save ptasker/3229701 to your computer and use it in GitHub Desktop.
Get attachments by slug
<?php
/**
*
* Gets the URL to a media item based on the media item's name (slug)
*
*
* @param type $slug
* @return type
*
*
*/
function get_attachment_by_slug( $slug ) {
if( empty( $slug ) )
return;
$query_images_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'name' => $slug
);
$query_images = new WP_Query( $query_images_args );
$out = array( );
if ( !empty( $query_images->posts ) ) {
foreach ( $query_images->posts as $image ) {
$out = wp_get_attachment_image_src( $image->ID, 'full' );
}
}
return isset($out[0])?$out[0]:'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment