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.
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.