I'm trying to redirect my attachment-id pages to the correct post-id page (woocommerce single page).
I got this URL with the attachment_id = 17381. How do I retrieve the products/post ID, which uses this attachment_id? I've searched for hours, and tried different approaches, but I can't find a solution.
https://www.ejstruplys.dk/no/?attachment_id=17381
I want it to be redirected to this page: https://www.ejstruplys.dk/no/produkt/block-candles-rustic-surface-3/
Doing an var_dump($post)
gives me this, but I can't figure out, where this attachment_id -> product ID is located:
OBJECT = WP_Post::__set_state(array(
'ID' => 17381,
'post_author' => '1',
'post_date' => '2015-08-24 19:32:00',
'post_date_gmt' => '2015-08-24 19:32:00',
'post_content' => '',
'post_title' => 'Orange. Bloklys. Rustikk overflate',
'post_excerpt' => '',
'post_status' => 'inherit',
'comment_status' => 'open',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => 'bloklys_rustik_overflade_55_dia_orange-jpg-2',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2015-08-24 19:32:00',
'post_modified_gmt' => '2015-08-24 19:32:00',
'post_content_filtered' => '',
'post_parent' => 0,
'guid' => 'https://www.ejstruplys.dk/wp-content/uploads/2015/08/bloklys_rustik_overflade_55_dia_orange.jpg',
'menu_order' => 0,
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'comment_count' => '0',
'filter' => 'raw',
'post_title_ml' => '[:en]Orange. Block candles. Rustic surface[:no]Orange. Bloklys. Rustikk overflate[:sv]Orange. Blokljus. Rustik yta[:de]Orange. Stumpenkerzen. Rustikale Oberfläche[:da]Orange. Bloklys. Rustik overflade[:]',
));
How do I retrieve the post ID from the attachment ID?
Edit: I've tried this one: Get post id by attachment id?
But it just returns null
.