How to fix Open Graph URL not matching canonical when using Yoast SEO

Ciprian on Thursday, August 11, 2022 in SEO

Canonical vs Open Graph URL

I have recently run into an issue with the Yoast SEO plugin, as reported by an Ahrefs audit.

Basically, the Yoast SEO plugin adds an og:url Open Graph tag with a canonical URL of the page. Everything works as expected, unless you are on a paginated archive or category page. For example, take the following code:

<link rel="canonical" href="https://www.example.com/category/news/page/2/">
<link rel="prev" href="https://www.example.com/category/news/">
<link rel="next" href="https://www.example.com/category/news/page/3/">
<meta property="og:locale" content="en_US">
<meta property="og:type" content="article">
<meta property="og:title" content="News Archives - Page 2 of 4 - My Awesome Website">
<meta property="og:description" content="See the latest news from My Awesome Website about all these awesome things.">
<meta property="og:url" content="https://www.example.com/category/news/">

Notice how the og:url is the main category page, not the actual page (2, 3 or whatever). This is the correct technical behaviour, but it’s not always the right one for our technical SEO. Yoast’s method doesn’t check if the current page (archive, category, or taxonomy) is paginated or not:

WPSEO_Frontend::get_instance()->canonical(false, true);

So we need to hook into this method and add the current page (if any). The code below checks if a variable called paged exists and then appends it to the current canonical URL:

function gb_wpseo_canonical( $canonical ) {
    global $post;

    $paged = get_query_var( 'paged' );

    if ( isset( $paged ) && (int) $paged > 0 ) {
        return trailingslashit( trailingslashit( $canonical ) . 'page/' . $paged );

        return $url;
    }

    return $canonical;
}

add_filter( 'wpseo_opengraph_url', 'gb_wpseo_canonical' );

The result is a matching og:url and a canonical tag:

<link rel="canonical" href="https://www.example.com/category/news/page/2/">
<link rel="prev" href="https://www.example.com/category/news/">
<link rel="next" href="https://www.example.com/category/news/page/3/">
<meta property="og:locale" content="en_US">
<meta property="og:type" content="article">
<meta property="og:title" content="News Archives - Page 2 of 4 - My Awesome Website">
<meta property="og:description" content="See the latest news from My Awesome Website about all these awesome things.">
<meta property="og:url" content="https://www.example.com/category/news/page/2/">

Related posts

6 comments on “How to fix Open Graph URL not matching canonical when using Yoast SEO

      1. You could put it in your theme, or in a custom plugin.

        What does the critical error say? There’s usually a hint there.

  1. Excellent. I faced this issue where “Open Graph URL not matching canonical” for paginated URLs. I just want to check your view if it is better to remove Oopengraph from such pages. Because it is less likely that such pages will be shared across social media. Would it cause or bring any negative impact? However, thanks for the code, it should resolve my issue, though, yet to apply it.

  2. Good. Implemented the given suggestion. From the code, the part “global $post;” can be removed as it is not needed. The changes are working fine as expected at my end. Sorry to bother you again, but thought to confirm the result.

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *