Make WordPress Core

Changeset 23776

Timestamp:
03/22/2013 08:08:46 AM (12 years ago)
Author:
helen
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r23774 r23776  
    17751775
    17761776/**
    1777  * Retrieve audio attached to the passed post
    1778  *
    1779  * @since 3.6.0
    1780  *
     1777 * Retrieve media attached to the passed post
     1778 *
     1779 * @since 3.6.0
     1780 *
     1781 * @param string $type (Mime) type of media desired
    17811782 * @param int $post_id  Post ID
    1782  * @return array Found audio attachments
    1783  */
    1784 function get_attached_audio( $post_id = 0 ) {
     1783 * @return array Found attachments
     1784 */
     1785function get_attached_ $post_id = 0 ) {
    17851786    $post = empty( $post_id ) ? get_post() : get_post( $post_id );
    17861787    if ( empty( $post ) )
    17871788        return;
    17881789
    1789     $children = get_children( array(
     1790    $ array(
    17901791        'post_parent' => $post->ID,
    17911792        'post_type' => 'attachment',
    1792         'post_mime_type' => 'audio',
    1793         'posts_per_page' => -1
    1794     ) );
    1795 
    1796     if ( ! empty( $children ) )
    1797         return $children;
     1793        'post_mime_type' => $type,
     1794        'posts_per_page' => -1,
     1795        'orderby' => 'menu_order',
     1796        'order' => 'ASC',
     1797    );
     1798
     1799    $args = apply_filters( 'get_attached_media_args', $args, $type, $post );
     1800
     1801    $children = get_children( $args );
     1802
     1803    return (array) apply_filters( 'get_attached_media', $children, $type, $post );
     1804}
     1805
     1806/**
     1807 * Retrieve audio attached to the passed post
     1808 *
     1809 * @since 3.6.0
     1810 *
     1811 * @param int $post_id  Post ID
     1812 * @return array Found audio attachments
     1813 */
     1814function get_attached_audio( $post_id = 0 ) {
     1815    return get_attached_media( 'audio', $post_id );
    17981816}
    17991817
     
    18071825 */
    18081826function get_attached_video( $post_id = 0 ) {
    1809     $post = empty( $post_id ) ? get_post() : get_post( $post_id );
    1810     if ( empty( $post ) )
    1811         return;
    1812 
    1813     $children = get_children( array(
    1814         'post_parent' => $post->ID,
    1815         'post_type' => 'attachment',
    1816         'post_mime_type' => 'video',
    1817         'posts_per_page' => -1
    1818     ) );
    1819 
    1820     if ( ! empty( $children ) )
    1821         return $children;
     1827    return get_attached_media( 'video', $post_id );
    18221828}
    18231829
     
    20042010 */
    20052011function get_attached_images( $post_id = 0 ) {
    2006     $post = empty( $post_id ) ? get_post() : get_post( $post_id );
    2007     if ( empty( $post ) )
    2008         return array();
    2009 
    2010     $children = get_children( array(
    2011         'post_parent' => $post->ID,
    2012         'post_type' => 'attachment',
    2013         'post_mime_type' => 'image',
    2014         'posts_per_page' => -1,
    2015         'orderby' => 'menu_order',
    2016         'order' => 'ASC'
    2017     ) );
    2018 
    2019     if ( ! empty( $children ) )
    2020         return $children;
    2021 
    2022     return array();
     2012    return get_attached_media( 'image', $post_id );
    20232013}
    20242014
Note: See TracChangeset for help on using the changeset viewer.