Fixing link rel="next" in Yoast SEO for paginated links

All we need is an easy explanation of the problem, so here it is.

I’ve got a site with yoast seo running, as seen in the source:

<title>Redacted</title>
<meta name="description" content="Redacted" />
<meta name="robots" content="index, follow" />
<meta name="googlebot" content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1" />
<meta name="bingbot" content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1" />
<link rel="canonical" href="Redacted" />
<link rel="next" href="Redacted.com/page/2/" />
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Redacted" />
<meta property="og:description" content="Redacted" />
<meta property="og:url" content="Redacted" />
<meta property="og:site_name" content="Redacted" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@Redacted" />
<!-- / Yoast SEO plugin. -->

However, where I have – it should actually be /index.php/page/2. That’s fine, I go to fix it.

However for the life of me I can’t work out where it’s setting it. It’s nowhere to be found in the Yoast plugin tools. I tried searching for the text across all plugins (comes up way too much to be useful).

I tried the yoast online documentation but they mostly talk about how meta tags aren’t used any more, and more about what canonical and next do, rather than how or where to set them.

Any advice appreciated on how to remedy this.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

In the current version of Yoast, this link is rendered by presenters/rel-next-presenter.php, which contains this filter:

    public function present() {
        $output = parent::present();
    
        if ( ! empty( $output ) ) {
          /**
           * Filter: 'wpseo_next_rel_link' - Allow changing link rel output by Yoast SEO.
           *
           * @api string $unsigned The full `<link` element.
           */
          return \apply_filters( 'wpseo_next_rel_link', $output );
        }
    
        return '';
    }

So, I’d suggest correcting this by implementing a filter in e.g. your functions.php, such as:

    add_filter( 'wpseo_next_rel_link', 'custom_change_wpseo_next' );

    function custom_change_wpseo_next( $oldLink ) {

        $new_link = 'https://example.com/index.php/page/2';
        $link = '<link rel="next" href="'. $new_link .'" />' . PHP_EOL;
        
        return $link;

    }

Does that do it?

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply