2014-10-09 38 views
0
获取媒体图像文件

我的所有媒体文件图像都被命名​​为'icon_0001','icon_0002','icon_0003','插图_0001','插图_0002','插图_0003'等,以及其他图像文件'SummonClear','StunPowder'等。我已经用wordpress admin ui和var_dumping附件检查了这一点。通过post_title

,现在我想用下面的代码获取媒体文件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但尚未附加到任何帖子。

回答

0

我想通了,找一找看帖子了什么是错误的,这要感谢Bizzkit的建议。我应该用来代替“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]; 
    } 

} 
2

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

你可以试试:

“名”由他们塞 “P”由“S” ID 进行关键字搜索

+0

Bizzkit /谢谢你的建议,但是这是一个媒体文件,我不认为媒体文件有“塞”? – Dongsan 2014-10-09 05:40:19

+0

他们这样做,您可以通过访问库来查看它,然后单击一个附件,然后选择“查看附件页面”或“编辑更多详细信息”。您可以在附件页面的URL中或附件编辑器页面的slug编辑插槽中(与在文章编辑页面上相同)中看到两个插件。让我知道如果这有帮助! – Yavor 2014-10-09 07:04:08