Solution:
Yes, WordPress has no way of working out whether an image you inserted into a post was actually attached to the post or not – it could be something you added from an external website for all it knows, it’s just HTML code in the editor.
As I think you have already identified, get_children will only find attachments – ie. photos that were uploaded for this post and not just inserted from the media library.
The only way to achieve what you are trying to do is to explicitly upload photos from with each post – meaning that if more than one post uses the same image, you’ll need to upload it again for each post. Not ideal, but at the moment that is the only way to “attach” an image to a post so that get_children works.
The only other way I could suggest to do it would be to use a custom field to manually store the post ID of the images you want to use as thumbnails (regardless of whether they are attached or not) and then iterate through those IDs when you want to display the thumbnail images for them. It’s a bit clunky, but would allow you to only upload photos once.
However, if all you are trying to do is show the post thumbnail, there is an easier way to do this. You can now separately specify an image to use as a “featured image” (any image from the library, doesn’t have to be attached to the post or displayed in the post).
First, make sure you add support for post thumbnails:
add_theme_support( 'post-thumbnails' );
Then go into your post, click “Add Media”, and choose “Set Featured Image” from the left menu. You can choose any image already in the library or upload a new one and it will be set at the post thumbnail.
In your template you can then just do something like this in the loop:
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
If you are working outside the loop, you can get the thumbnail ID for a specific post:
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
… or just get the thumbnail HTML directly:
get_the_post_thumbnail( $post_id, $size, $attr );
Unfortunately, you can only have one featured image, so if you need more, you’ll need to go back to your original code and explicitly attach images to the post.