I have tried this code to get all images from media library and i am sucessfully getting the source urls of all the images but now i want to exclude all the unwanted images like logo, header images ,etc ...

In short i want to extract all images attached to posts and pages ..

if(is_single() || is_page() || is_home() ){  
          global $post;  

$query_images_args = array(
     'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,'numberposts' => 1

 $query_images = new WP_Query( $query_images_args );
 $images = array();
         foreach ( $query_images->posts as $image) {
         $images[]= wp_get_attachment_url( $image->ID );

            echo "<pre>";
             echo "</pre>"; 

My output Here the first image is a header image which is unwanted for me ..How to exclude it ..I have tried using attachment size but it cant be unique all the time .. Have a look at it

    [0] => http://localhost/wordpress/wp-content/uploads/2013/03/AboutUsSlider.jpg
    [1] => http://localhost/wordpress/wp-content/uploads/2013/03/7325996116_9995f40082_n.jpg
    [2] => http://localhost/wordpress/wp-content/uploads/2013/03/6310273151_31b2d7bebe.jpg
    [3] => http://localhost/wordpress/wp-content/uploads/2013/03/4764924205_ce7470f15a.jpg
    [4] => http://localhost/wordpress/wp-content/uploads/2013/03/2166105529_70dd50ef4b_n.jpg
    [5] => http://localhost/wordpress/wp-content/uploads/2013/03/1494822863_aca097ada7.jpg
    [6] => http://localhost/wordpress/wp-content/uploads/2013/03/1385429771_453bc19702.jpg
  • This is from the related questions section on the right. Maybe it helps you determine which images are actually attached to a post, and which images are uploaded through Media Library but are "floating" there unattached. Mar 29, 2013 at 9:22
  • Thanks @MarcDingena But it doesnt really meet my requirement..Bcoz i also want the images which are inserted in post content and page content... Mar 29, 2013 at 9:57
  • Images inserted into post content by the media tools should be attached to the posts. There may be no generic solution for what you are asking. Without knowing how your images are uploaded and attached, or not attached, it may not be possible to construct the criteria you need to eliminate these images. In fact, there may not be clear criteria that you can put into code-- other than hard code the exceptions. I think you need to add more detail concerning these points.
    – s_ha_dum
    Mar 29, 2013 at 13:29
  • @s_ha_dum I agree with your logically it is impossible to differentiate between images on basis of our criteria like logo or post content.. Can we just grab images of only post contents or page contents ..This is my imagination like we can just grab all images from all page templates with img tags while going through the contents of it..which might remove those unwanted images ...Or another way round is differentiate the images with its size ..second method do have some exceptions ..Anyways i have changed my logic ..Thanks for your knowledge.. Mar 30, 2013 at 6:35

1 Answer 1


I believe this will get you all of your attachments:

$q = new WP_Query(

You could further restrict that by adding other parameters, for example, 'post_mime_type' => 'image' would restrict only to images.

That will return all images though, not just the ones attached to posts. What you want is post_parent != 0 but I don't see a way to do that with WP_Query. If you var_dump that query though you can steal its SQL and add the parameter, or build a posts_where filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.