检索附加到给定帖子ID的附件ID数组

时间:2013-12-21 作者:florian norbert bepunkt

我想知道是否有一个函数可以检索包含与给定帖子匹配的图像的附件ID的数组。一些函数,如get\\u post\\u arry($pid);

数组的顺序如下

    [\'0\':\'image1\', \'1\':\'image2\', \'2\':\'image3\', .... etc. ]
任何帮助都将不胜感激。

谢谢你,弗洛里安

1 个回复
SO网友:florian norbert bepunkt

找到了一个解决方案:认为它可能值得分享。。。

////  We use $gallery_media as an array containing all attachment IDs that have been approved by the admin before
    $args = array(
        \'post_type\' => \'attachment\',
        \'posts_per_page\' => -1,
        \'orderby\'          => \'post_date\',
        \'order\'            => \'ASC\',
        \'post_status\' =>\'any\',
        \'post_parent\' => $post->ID,
        // This queries a custom field I created for media attachments – can be either \'piúblished\' or \'pending\'
        \'meta_query\' => array(
            array(
                \'key\' => \'attachment_status\',
                \'value\' => \'published\',
            )
        )
        ); 
    $attachments = get_posts( $args );
    if ( $attachments ) {
        $gallery_media = array();
        foreach ( $attachments as $attachment ) {
            // Check if attachment has been reviewed and custom status is "published"
            // Put together our array
            $gallery_media[] = apply_filters( \'id\' , $attachment->ID );
        }
    }

// Outputs an indexed array containing attachment IDs according to our query

http://pastebin.com/ADVkaFvU

结束

相关推荐

WP_Query in functions.php

我有一些代码要转换成函数。它工作得很好,直到我将其包装到所述函数中: $args = array( \'posts_per_page\' => -1, \'post_type\' => \'asset\', \'category_name\' => $cat ); $cat_query = new WP_Query( $args );