All my media file images are named like ‘icon_0001’, ‘icon_0002’, ‘icon_0003’, ‘illustration_0001’, ‘illustration_0002’, ‘illustration_0003’ and so on, along with other image files like ‘SummonClear’, ‘StunPowder’, etc. I’ve checked this with the wordpress admin ui and also by var_dumping the attachments.
And now I am trying to retrieve the media file ID with the following code:
$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/>';
}
Yes, this is just a simple query code using post_type = attachment
.
However, whatever name I give to $post_name
, the query displays ALL image files in the media library, like below:
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.
I know I have a media file with the name ‘illustration_30004’ because below result is included in the 900+ results.
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" }
So the question is…what is wrong with my query and why am I getting ALL media files instead of the post_title
I want?
Added: one more thing though i’m not sure this is relevant – these media files are uploaded to wordpress but are not attached to any post yet.
I don’t think ‘post_title’ is a valid argument. Parameters
You can try:
‘name’ to look posts up by their slug
‘p’ to look them up by ID
‘s’ to perform a keyword search
I figured out what was wrong, thanks to the suggestion from Bizzkit. I should have used ‘name’ instead of ‘post_title’, as below: