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.
Related Posts:
- WordPress 3.4 Permalinks Redirection Problem
- Permalinks settings
- How to redirect old permalinks from https://exmaple.com/category/post_id to https://exmaple.com/category/post_id.html
- remove “index.php” from permalinks
- How does WordPress create URLs that Apache knows about?
- Stop WordPress appending `-2` to the end of my url after i change it
- Get default permalink structure from pretty URL’s
- Get current URL (permalink) without /page/{pagenum}/
- How does WordPress handle permalinks?
- How to get pretty URLs with add_query_arg in permalinks
- Creating custom permalink structure for languages
- Getting the Site URL Including the Front Base
- Removing the redirect after changing a page’s slug
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- How to use relative links on my pages?
- WordPress Permalink changes to question mark (?) in URL
- Adding another word in front of post url
- How to prevent apostrophes and quotes from appearing in permalinks?
- Removing hierarchical pages in the permalink
- 301 Redirects for Changed Permalink Structure & Category Base
- URL rewrite based on a custom field value
- Update URL Snippet to Canonical Permalink URL
- why does anchor name add a slash to url?
- How can I force WordPress to redirect to canonical permalinks?
- How do I turn off 301 redirecting posts (not canonical)?
- “.#[random-char-string]” being inserted at end of URLs
- Subdomains to pages
- Redirect old permalinks to new permalinks
- Passing variables through permalink structure
- + Character in permalink
- URL Rewrite + Page + Custom Post Type = Unusual Redirect
- WordPress on a subdirectory of Laravel – WordPress pretty permalinks inner page shows laravel
- Pretty Permalinks
- How to create a permalink structure for posts in a specific category
- How to get url of a post from admin panel
- How can I include a query string with get_permalink
- How to Remove Slug From Database?
- How to make draft posts or posts in review accessible via full url / slug?
- Permalink redirection from ‘Day and name’ to ‘Post name’
- Remove subfolders from URL
- Nicest way to 301 Redirect traffic when changing permalink settings
- Limiting the number of words or characters in the slug/permalink
- flexible rewrite ‘ramble’ URLs with WordPress
- Changing pemalink structure to /%post_id%/%postname%/
- How to increase the character limit for post name of 200?
- htaccess or redirect to cloak portion of a link?
- How to get the post id from a permalink?
- Redirect short-form URL to long-form URL (post_id to post_id + post_name)
- Custom rewrite rules for feeds of custom queries (query_var query strings in URL)?
- Auto 301 to full post permalink? (using /posts/%post_id%/%postname%)
- Change website URL without breaking links or images? WP 3.3
- WordPress on VirtualBox – no pretty permalinks
- Stop redirecting to posts not in a category / duplicate slugs
- Random Alphanumeric Key URLs
- Redirect old permalinks with Month and name to Post name on new site
- Make attachment pages require a base url
- Rewrite-Rules not working on a vhost, everything goes to index.php
- Using WP rather than .htaccess to redirect pages/posts
- Does WordPress send a 301 header message when you change permalink structures?
- How to Remove -14 from WordPress URL (Permalink)
- How to Modify Existing Rewrite Rules?
- .htaccess – Redirect duplicated post ended in ‘-number/’ to the same url without the -number/
- Top level Pages redirecting to homepage!
- Using ‘Primary Category’ in URL
- display URL or permalink instead of page title in dashboard
- custom permalink/shortlink with base62 encoded post ID
- WordPress redirection problem after moving to a new server?
- How can I get the permalink of a post before it is published?
- Specific URL Rewriting with parameters in wordpress
- Change permalink rule if category is called “uncategorized”
- Add a URL prefix to permalinks of News category and sub-categories of posts only
- Can two products with the same name but different category produce unique URLs?
- How to 301 all posts
- WordPress within specific sub-directories, implicitly not root
- echo post-permalink without http and www
- Confusion about how a URL is being parsed
- Get closest page ID from URL
- How to reset canonical URLs / fix redirect loop
- CSS not loaded when omitting www. part of URL
- Setting up two separate blogs while using WordPress as a CMS
- How to show the homepage on a different url, like site.com/blog instead of site.com?
- Post incorrectly displaying as page
- Custom permalink for search and pagination
- wp_logout_url redirects to incorrect page because of pagination
- If I change permalink structures, can I use htaccess to permanantly redirect links?
- Redirect parent taxonomy to it’s child
- Changed permalink structure. Need help with redirecting old posts
- Need Help Correct Regular Expression Redirect Code
- wordpress api using rest_route for other pages
- Infinite redirects after changing the permalink of a page
- Get page by path – honoring permalink settings (urls w/slashes)
- Does rename of page slugs create 301 redirects from old slug to new (same as for posts)?
- Change blog segment of URL
- Custom Permalink
- How to stop WordPress from removing & from URL?
- Changing the post date without causing 404 error
- Change permalinks on move from one site to another
- Non latin post slug truncate
- Force wordpress to display page with same url structure as categories
- List / Get post redirects