As you’re trying to get the ID of an attachment, functions like get_post_thumbnail_id()
won’t work on an attachment post because itself is the attachment.
You should be able to get the ID simply with
$post_id = $attachment->ID;
As you’re trying to get the ID of an attachment, functions like get_post_thumbnail_id()
won’t work on an attachment post because itself is the attachment.
You should be able to get the ID simply with
$post_id = $attachment->ID;