通过post_title获取媒体图像文件

时间:2014-10-09 05:16:22

标签: php wordpress args wp-query

我的所有媒体文件图像都被命名​​为'icon_0001','icon_0002','icon_0003','illustration_0001','illustration_0002','illustration_0003'等,以及其他图像文件,如'SummonClear','StunPowder '等我用wordpress admin ui和var_dumping附件检查了这个。

现在我正尝试使用以下代码检索媒体文件ID:

$post_name = 'illustration_30004';
$args = array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'post_title' => trim( $post_name ),
    'post_mime_type' => 'image/png',
    'posts_per_page' => -1
    );


$wp_query = new WP_Query( $args );

foreach( $wp_query->posts as $image) {
    var_dump( $image );
    echo '<hr/>';

}

是的,这只是一个使用post_type = attachment的简单查询代码。 但是,无论我给$post_name提供什么名称,查询都会在媒体库中显示所有图像文件,如下所示:

object(WP_Post)#1386 (24) { ["ID"]=> int(40735) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2014-10-08 10:22:04" ["post_date_gmt"]=> string(19) "2014-10-08 10:22:04" ["post_content"]=> string(0) "" ["post_title"]=> string(11) "SummonClear" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "inherit" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(13) "summonclear-3" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2014-10-08 10:22:04" ["post_modified_gmt"]=> string(19) "2014-10-08 10:22:04" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(104) "http://101.251.201.22/digital-opal-mu/elin-expedition/wp-content/uploads/sites/2/assets/SummonClear2.png" ["menu_order"]=> int(0) ["post_type"]=> string(10) "attachment" ["post_mime_type"]=> string(9) "image/png" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

object(WP_Post)#1387 (24) { ["ID"]=> int(40734) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2014-10-08 10:22:04" ["post_date_gmt"]=> string(19) "2014-10-08 10:22:04" ["post_content"]=> string(0) "" ["post_title"]=> string(11) "SummonChick" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "inherit" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(13) "summonchick-3" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2014-10-08 10:22:04" ["post_modified_gmt"]=> string(19) "2014-10-08 10:22:04" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(104) "http://101.251.201.22/digital-opal-mu/elin-expedition/wp-content/uploads/sites/2/assets/SummonChick2.png" ["menu_order"]=> int(0) ["post_type"]=> string(10) "attachment" ["post_mime_type"]=> string(9) "image/png" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

object(WP_Post)#1388 (24) { ["ID"]=> int(40733) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2014-10-08 10:22:04" ["post_date_gmt"]=> string(19) "2014-10-08 10:22:04" ["post_content"]=> string(0) "" ["post_title"]=> string(10) "StunPowder" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "inherit" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(12) "stunpowder-3" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2014-10-08 10:22:04" ["post_modified_gmt"]=> string(19) "2014-10-08 10:22:04" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(103) "http://101.251.201.22/digital-opal-mu/elin-expedition/wp-content/uploads/sites/2/assets/StunPowder2.png" ["menu_order"]=> int(0) ["post_type"]=> string(10) "attachment" ["post_mime_type"]=> string(9) "image/png" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

object(WP_Post)#1389 (24) { ["ID"]=> int(40732) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2014-10-08 10:22:04" ["post_date_gmt"]=> string(19) "2014-10-08 10:22:04" ["post_content"]=> string(0) "" ["post_title"]=> string(5) "Stomp" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "inherit" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(7) "stomp-3" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2014-10-08 10:22:04" ["post_modified_gmt"]=> string(19) "2014-10-08 10:22:04" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(98) "http://101.251.201.22/digital-opal-mu/elin-expedition/wp-content/uploads/sites/2/assets/Stomp2.png" ["menu_order"]=> int(0) ["post_type"]=> string(10) "attachment" ["post_mime_type"]=> string(9) "image/png" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

...and 900+ more of these.

我知道我有一个名为'illustration_30004'的媒体文件,因为以下结果包含在900多个结果中。

object(WP_Post)#1871 (24) { ["ID"]=> int(40973) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2014-10-07 03:38:14" ["post_date_gmt"]=> string(19) "2014-10-07 03:38:14" ["post_content"]=> string(0) "" ["post_title"]=> string(18) "illustration_30004" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "inherit" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(18) "illustration_30004" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2014-10-07 03:38:14" ["post_modified_gmt"]=> string(19) "2014-10-07 03:38:14" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(110) "http://101.251.201.22/digital-opal-mu/elin-expedition/wp-content/uploads/sites/2/assets/illustration_30004.png" ["menu_order"]=> int(0) ["post_type"]=> string(10) "attachment" ["post_mime_type"]=> string(9) "image/png" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

所以问题是......我的查询出了什么问题,为什么我得到所有媒体文件而不是我想要的post_title

补充:还有一件事,虽然我不确定这是否相关 - 这些媒体文件已上传到wordpress但尚未附加到任何帖子。

2 个答案:

答案 0 :(得分:2)

我不认为'post_title'是一个有效的参数。 Parameters

您可以尝试:

'name'以查看帖子的帖子 'p'用ID查找它们 's'执行关键字搜索

答案 1 :(得分:0)

由于Bizzkit的建议,我弄清楚出了什么问题。我应该使用'name'而不是'post_title',如下所示:

/**
 * Get media image file by name
 */
function get_media_image( $image_name ) {

    $post_name = trim( $image_name );

    $args = array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'name' => $post_name,
        'post_mime_type' => 'image/png',
        'posts_per_page' => -1
        );


    $wp_query = new WP_Query( $args );
    $images = array();

    foreach( $wp_query->posts as $image) {
        $images[] = $image->guid;
    }

    if( sizeof( $images ) == 1 ) {
        return $images[0];
    }

}