[wp-trac] [WordPress Trac] #63041: wp_get_canonical_url always returns false for attachments
WordPress Trac
noreply at wordpress.org
Sat Mar 1 17:19:18 UTC 2025
#63041: wp_get_canonical_url always returns false for attachments
--------------------------+-----------------------------
Reporter: othernoel | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Canonical | Version: 6.4.3
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
The code for wp_get_canonical_url makes this check
{{{#!php
if ( 'publish' !== $post->post_status ) {
return false;
}
}}}
When $post is an attachment, post_status is likely 'inherit' and never
'publish', meaning this check always fails.
Consequently, if attachments have their own pages, those pages don't get a
rel="canonical" printed in wp_head, and the 'get_canonical_url' filter is
never called for attachment pages. Is this intended behavior?
I'd assume get_post_status() to be the correct function here, as it
resolves 'inherit' in attachments returning the parent post's status?
I haven't seen this documented anywhere, so I'm not sure if this way for
backwards compatibility or an actual bug.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/63041>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list