получить файл медиа-изображения по post_title

Все мои изображения мультимедийных файлов имеют такие имена, как «icon_0001», «icon_0002», «icon_0003», «illustration_0001», «illustration_0002», «illustration_0003» и т. Д., А также другие файлы изображений, такие как «SummonClear», «StunPowder» и т. Д. Я проверил это с помощью интерфейса администратора WordPress, а также с помощью var_dumping вложений.

И теперь я пытаюсь получить идентификатор медиа-файла с помощью следующего кода:

$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

Решение

Я не думаю, что ‘post_title’ является действительным аргументом. параметры

Ты можешь попробовать:

‘name’, чтобы искать сообщения по их слизнякам
‘p’, чтобы найти их по ID
‘s’, чтобы выполнить поиск по ключевым словам

2

Другие решения

Я понял, что случилось, благодаря предложению от 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];
}

}
0