Duplicate content in WordPress: canonical URLs and noindex
The same post can be reached under several URLs: with tracking parameters, with ?replytocom=, with a paged comments suffix. Archives repeat the same excerpts on category, tag, date and author pages. WordPress already outputs a canonical link on single posts and pages; the work is to extend it where it is missing and to keep the low-value archives out of the index, without replacing what core does right.
Problem: Search Console reports “Duplicate without user-selected canonical” for archive pages and URLs with parameters, and thin archives (date, author, internal search) end up in the index.
Solution: Keep the core rel_canonical() for singular pages and adjust it with the get_canonical_url filter, add a canonical link for term archives yourself, and mark the low-value archives with noindex, follow through the wp_robots filter (WordPress 5.7+).
Background on titles and canonical links: wrong title and missing canonical link.
What core does
On posts, pages and attachments WordPress prints <link rel="canonical"> with the permalink (and the comments page if comments are paged). Query parameters are not part of it, so ?utm_source=... is already handled. Archives get no canonical link at all.
Adjust the singular canonical
add_filter( 'get_canonical_url', function ( $url, $post ) {
// A landing page duplicated from another one points to the original
$original = (int) get_post_meta( $post->ID, '_ha_canonical_post', true );
return $original ? get_permalink( $original ) : $url;
}, 10, 2 );
Canonical for term archives
add_action( 'wp_head', 'ha_archive_canonical', 5 );
function ha_archive_canonical() {
if ( ! ( is_category() || is_tag() || is_tax() ) ) {
return;
}
$url = get_term_link( get_queried_object() );
if ( is_wp_error( $url ) ) {
return;
}
$paged = (int) get_query_var( 'paged' );
if ( $paged > 1 ) {
$url = user_trailingslashit( trailingslashit( $url ) . 'page/' . $paged );
}
echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
}
Each page of an archive points to itself, not to page 1: page 2 lists other posts than page 1, and a canonical to page 1 tells Google to ignore the posts that are only linked from page 2. The canonical removes the parameters and the duplicates, nothing else.
noindex for thin archives
add_filter( 'wp_robots', function ( $robots ) {
if ( is_search() || is_date() || is_author() ) {
$robots['noindex'] = true;
$robots['follow'] = true;
}
if ( is_tag() && get_queried_object()->count < 3 ) {
$robots['noindex'] = true;
$robots['follow'] = true;
}
return $robots;
} );
wp_robots collects all directives in one <meta name="robots"> tag, so the result does not conflict with the tag core adds when “Discourage search engines” is on. follow lets the crawler still use the links on the page.
Do not combine both on one page
A page with noindex and a canonical pointing to another URL sends two different signals. Use the canonical for duplicates of a page that should rank, and noindex for pages that should not rank at all.
NOTE: SEO plugins (Yoast, Rank Math) replace rel_canonical() and the robots tag with their own output. With such a plugin active, use its settings instead of this code, or the page ends up with two canonical links.