1

Hi I know get_permalink returns the URL. Some of my posts are redirected and at the same time I update the canonical link to the redirected post URL - (normally relates to post updates).

Is there a function I can use to return the canonical URL of a post if it has one rather than it's 'permalink' URL? I use Yoast SEO to edit canonical URLs and also apply redirects. I want to use the 'canonical permalink' in a function.

2
  • 1
    Unless I am missing something, terminologically permalink is canonical permalink in modern WordPress. Could you elaborate (maybe with example) why/how do you consider them to be different?
    – Rarst
    Feb 9, 2014 at 17:43
  • Yes no problem. Yoast SEO allows posts to be redirected to avoid duplicate content and at the same time the canonical link can be set to the redirected post. On my site take this archive page skipology.com/category/iphoneography-workflow - the first post skipology.com/… redirects to the second post (it is an update) skipology.com/atmospheric-photography-fog-cant-find-create - get_permalink returns the first post URL but I need to return the link I have set as canonical that it redirects to (second link).
    – Paul Brown
    Feb 9, 2014 at 23:18

1 Answer 1

3

Since WP 4.6 there has been a core function for this: wp_get_canonical_url()

Call it with a post ID to get that post's canonical URL, or in the loop without a post ID to get the current post's canonical URL.

Your Answer

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

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