How to extract images of post and pages excluding header and logo image in wordpress?

I believe this will get you all of your attachments:

$q = new WP_Query(
  array(
    'post_type'=>'attachment',
    'post_status'=>'inherit'
  )
);
var_dump($q);

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.

Leave a Comment