Add filter on wp_title not working with Yoast SEO plugin

A SEO filter on wp_title

Febbraio 22, 2017

For SEO purposes, in a specific template that has a list of items with pagination, I need to have different urls and titles. Urls change using .../page/2/, .../page/3/ and the paging is handled with get_query_var('paged'). But Yoast SEO plugin doesn’t add automatically the number to the title so in the Google Search Console these pages are considered duplicates.
This code in the functions.php fixes the problem, the number “50” in the add_filter call refers to the priority, so this filter runs after the Yoast’s one and do the work!

function modify_title_with_pagination( $title ) {
	if(get_query_var('paged') && get_query_var('paged') > 0) {
		if ( is_page_template( 'your-template.php' )) {
			return $title . " (page ".get_query_var('paged').")";
		}
	}
    return $title;
}
add_filter( 'wp_title', 'modify_title_with_pagination', 50,2 );

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.