Title: get_canonical_url
Published: August 16, 2016
Last modified: February 24, 2026

---

# apply_filters( ‘get_canonical_url’, string $canonical_url, WP_Post $post )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#wp--skip-link--target)

Filters the canonical URL for a post.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#parameters)󠁿

 `$canonical_url`string

The post’s canonical URL.

`$post`[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)

Post object.

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#source)󠁿

    ```php
    return apply_filters( 'get_canonical_url', $canonical_url, $post );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/link-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/link-template.php#L4106)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/link-template.php#L4106-L4106)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#related)󠁿

| Used by | Description | 
| [wp_get_canonical_url()](https://developer.wordpress.org/reference/functions/wp_get_canonical_url/)`wp-includes/link-template.php` |

Returns the canonical URL for a post.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#changelog)󠁿

| Version | Description | 
| [4.6.0](https://developer.wordpress.org/reference/since/4.6.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/hooks/get_canonical_url/?device=c&output_format=md#comment-content-5669)
 2.   [Matt Calvert](https://profiles.wordpress.org/xandercalvert/)  [  4 years ago  ](https://developer.wordpress.org/reference/hooks/get_canonical_url/#comment-5669)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_canonical_url%2F%23comment-5669)
    Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_canonical_url%2F%23comment-5669)
 4. An example of using this filter. In this I am setting the canonical url through
    ACF in a field called ‘canonical_url’.
 5.     ```php
        function wpdocs_edit_canonical_urls( $original_url, $post ) {
        	$override_url = get_post_meta( $post->ID, 'canonical_url', true );
    
        	if ( empty( $override_url ) ) {
        		return $original_url;
        	}
    
        	return $override_url;
    
        }
    
        add_filter( 'get_canonical_url', 'wpdocs_edit_canonical_urls', 10, 2 );
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_canonical_url%2F%3Freplytocom%3D5669%23feedback-editor-5669)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_canonical_url%2F)
before being able to contribute a note or feedback.