Add a generic `get_attached_media()` function and use it in get_attached_audio|video|images. Add filters for the query args and resulting array. fixes #23843.

git-svn-id: http://core.svn.wordpress.org/trunk@23776 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Helen Hou-Sandí 2013-03-22 08:08:46 +00:00
parent 3f5f4fa62e
commit b2f0b89062
1 changed files with 33 additions and 43 deletions

View File

@ -1773,6 +1773,36 @@ function wp_enqueue_media( $args = array() ) {
do_action( 'wp_enqueue_media' ); do_action( 'wp_enqueue_media' );
} }
/**
* Retrieve media attached to the passed post
*
* @since 3.6.0
*
* @param string $type (Mime) type of media desired
* @param int $post_id Post ID
* @return array Found attachments
*/
function get_attached_media( $type, $post_id = 0 ) {
$post = empty( $post_id ) ? get_post() : get_post( $post_id );
if ( empty( $post ) )
return;
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => $type,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$args = apply_filters( 'get_attached_media_args', $args, $type, $post );
$children = get_children( $args );
return (array) apply_filters( 'get_attached_media', $children, $type, $post );
}
/** /**
* Retrieve audio attached to the passed post * Retrieve audio attached to the passed post
* *
@ -1782,19 +1812,7 @@ function wp_enqueue_media( $args = array() ) {
* @return array Found audio attachments * @return array Found audio attachments
*/ */
function get_attached_audio( $post_id = 0 ) { function get_attached_audio( $post_id = 0 ) {
$post = empty( $post_id ) ? get_post() : get_post( $post_id ); return get_attached_media( 'audio', $post_id );
if ( empty( $post ) )
return;
$children = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'audio',
'posts_per_page' => -1
) );
if ( ! empty( $children ) )
return $children;
} }
/** /**
@ -1806,19 +1824,7 @@ function get_attached_audio( $post_id = 0 ) {
* @return array Found video attachments * @return array Found video attachments
*/ */
function get_attached_video( $post_id = 0 ) { function get_attached_video( $post_id = 0 ) {
$post = empty( $post_id ) ? get_post() : get_post( $post_id ); return get_attached_media( 'video', $post_id );
if ( empty( $post ) )
return;
$children = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'video',
'posts_per_page' => -1
) );
if ( ! empty( $children ) )
return $children;
} }
/** /**
@ -2003,23 +2009,7 @@ wp_embed_register_handler( 'wp_video_embed', '#https?://.+?\.(' . join( '|', wp_
* @return array Found image attachments * @return array Found image attachments
*/ */
function get_attached_images( $post_id = 0 ) { function get_attached_images( $post_id = 0 ) {
$post = empty( $post_id ) ? get_post() : get_post( $post_id ); return get_attached_media( 'image', $post_id );
if ( empty( $post ) )
return array();
$children = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
) );
if ( ! empty( $children ) )
return $children;
return array();
} }
/** /**