3

I have attachment id, but can't find way to get post id by this attachment id. I can loop all posts and find it, but maybe exists easy way get it. Does anyone know a similar function?

1 Answer 1

10

get_post_ancestors can give you the ID of the object an attachment is associated with:

$attachment_id = 42;
$parent = get_post_ancestors( $attachment_id );
echo $parent[0]; // $parent will be an array
0

Your Answer

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

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