I want to get all inline images in the post content.

The get_children() method doesn't work for me because they could add the same photo twice. Also this only gets "attached" images, and they could possibly add an image through the HTML editor tab which doesn't attach it to the post.

I want to get an array of all inline images in the order they appear in the post.

Is this possible?


1 Answer 1


Well you can do this 1 of 2 ways.

$attachments = get_posts(array(
                    'post_type' => 'attachment',
                    'numberposts' => -1,
                    'post_status' => null,
                    'post_parent' => get_the_ID(),
                    'order' => 'ASC',
                    'orderby' => 'id',


And then manipulate the array to remove duplicates.

Another option would be to apply a content filter, and use a regex to find and grab all img tags within the content.

As far as I know, when you add an image in the visual editor, wp adds it as a post attachment, thus you shouldn't end up with duplicates via the first method. As for grabbing them in the order they appear, attachments are added sequentially, so grabbing the list by ID should give you the list in the order they were added to the post, but not necessarily in the order they appear in the markup. For that, you'd definitely have to go with the second method of using a content filter.

I don't personally like the idea of using a content filter for this though, because it might be difficult to write a proper regex to grab the imgs, depending on what all markup is added.

For a regex, something like this:

preg_match('#(<img.*?>)#', $content, $results);

might work. This will dump the results into the $results variable as an array you can work with.

  • 1
    Getting the images as attachments from the database wont work for me, as I explain above they could manually enter an image through the HTML tab. I'm probably going to need some sort of regex to search through the post content.
    – Corey
    Jun 25, 2015 at 19:31
  • Yeah, that would be the only option in that case unfortunately. Jun 25, 2015 at 19:35

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