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
- Get default permalink structure from pretty URL’s
- Getting the Site URL Including the Front Base
- How to use relative links on my pages?
- WordPress Permalink changes to question mark (?) in URL
- How to prevent apostrophes and quotes from appearing in permalinks?
- Update URL Snippet to Canonical Permalink URL
- How do I turn off 301 redirecting posts (not canonical)?
- “.#[random-char-string]” being inserted at end of URLs
- 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 get url of a post from admin panel
- How can I include a query string with get_permalink
- Nicest way to 301 Redirect traffic when changing permalink settings
- Limiting the number of words or characters in the slug/permalink
- How to get the post id from a permalink?
- Redirect short-form URL to long-form URL (post_id to post_id + post_name)
- Change website URL without breaking links or images? WP 3.3
- Stop redirecting to posts not in a category / duplicate slugs
- Random Alphanumeric Key URLs
- Rewrite-Rules not working on a vhost, everything goes to index.php
- Using WP rather than .htaccess to redirect pages/posts
- Using ‘Primary Category’ in URL
- display URL or permalink instead of page title in dashboard
- custom permalink/shortlink with base62 encoded post ID
- How can I get the permalink of a post before it is published?
- Add a URL prefix to permalinks of News category and sub-categories of posts only
- How to 301 all posts
- WordPress within specific sub-directories, implicitly not root
- echo post-permalink without http and www
- CSS not loaded when omitting www. part of URL
- Custom permalink for search and pagination
- If I change permalink structures, can I use htaccess to permanantly redirect links?
- Redirect parent taxonomy to it’s child
- 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?
- Change permalinks on move from one site to another
- Non latin post slug truncate
- List / Get post redirects
- How to bulk edit image url in featured image and product description?
- Redirects after permalink change
- How to 301 redirect from one url to another properly
- Redirect a Blogger Page URL to wordpress
- How to include sub directory in sitemap? [closed]
- Remove -2 from URL; no duplicate pages, no images, no pages in trash
- Htaccess https 301 problem
- 301 Redirect for Non-Date Based URL’s
- Passing the JQuery string to Worldpress URL
- mod-rewrite exception? keep #hash in matching urls?
- Edit permalink for certain pages
- Some Pemalink problems, probably caused by wrong .htaccess configuration?
- Suddenly new posts have the default permalink instead of the post name
- Comment author url link is wrong? How to change from authors/ to author/
- Strange behavior with a redirect loop – involves WPML multi-language plugin
- Two URLs of one post is available
- Redirecting “wrong” but working links to their canonical URL in order to prevent search engine penalties (e.g. /123/ at the end of URLs)
- Arabic permalink redirect to home page
- Getting an ID from a URL, why can WP do this itself, but none of of the functions provided get it right?
- Custom permalink structure with %postname% in front of domain name
- Configuring Home Page Address
- Can I change the URL structure for a CPT only?
- How do I structure my permalinks in WordPress?
- corrupted URLs – can’t leave starting page
- Make a post inaccessible without a unique ID in the URL
- Customize URL for specific tags archive
- URL gives 404 error of my subfolder site when i change permalink
- Redirect old /%category%/%postname%/ to just /%postname%/
- why wordpress keeps redirecting to old page after deleting it?
- Why does one of my pages not have the option to edit the link?
- Giving specific category posts its own permalink structure returns 404
- Adding query string to multiple page urls in a WordPress function
- 301 redirects after changing permalinks
- Post links broken after migration
- I have Akamai pointing to a WordPress installation with a different URL. Is there a way to make WordPress use this for link generation?
- Change permalink structure with post id
- Added a prefix in URL permalink structure but the old structure still working!
- Difference and usage of uri (e.g. get_directory_uri) and absolute path (e.g. get_directory)
- changing permalink gives 404 error for old links
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Issues with WordPress permalinks
- How to display post permalinks instead of pages?
- WordPress permalink still not working (error or not understood?)
- Why is there a # and other characters in URL for WordPress site?
- Removed and Reinstalled WordPress, Redirect Broken now
- Pagination is not working, how to trace it properly?
- Update permalinks after domain change
- redirect old pages by .htaccess
- Why are all pages redirecting to homepage when using custom permalinks?
- How to avoid duplicate Url for the home page
- .htaccess regex for redirect subdomain to root domain & redirect old permalinks
- Removing parent page in URL for one page
- Remove parent product category slug from product_cat url woocommerce
- 301 Redirect posts to newly added categories
- WordPress not remembering old slugs for pages