Adding WordPress posts to more than one category can cause duplicate content issues. Here's how to fix this with automatic redirects using an SEO plugin such as Rank Math and a WordPress function.
If you put a WordPress post in more than one category and your custom permalink structure includes the category name, then your post will be available at more than one URL.
Let me give you an example from this site. My custom permalink structure (as set in the WordPress dashboard, Settings > Permalinks) is: /%category%/%postname%/
My custom permalink structure includes the /%category%/ tag
I created a post and added it both my 'Analytics' and 'SEO' categories. This meant it was available at two different URLs (permalinks):
This is, by definition, duplicate content - the same content available in more than one location. This can cause SEO issues, for example you might want Google to index one version but it chooses to index a different one. Also, the benefit of any inbound links is diluted by being spread across multiple URLs.
Many of the SEO issues caused by placing a post in multiple categories can be mitigated by using a WordPress SEO plugin such as Rank Math or Yoast SEO (the free version of either will do). These plugins allow you to select one of your chosen categories as the 'primary category'.
The exact mechanism for doing this differs slightly depending on how you create your WordPress posts. For example, if you use the Classic Editor, Rank Math adds a radio button to the Categories section of the 'Edit Post' page:
If you prefer to use the newer Block Editor, Rank Math adds a dropdown called 'SELECT PRIMARY TERM' instead:
However you do it, selecting a primary category adds a so-called canonical tag to each of the pages indicating which is the main or 'canonical' one. (Technically speaking, it's a <link> element with the attribute rel="canonical", as you'll see in the next screengrab.)
As well as telling Google which version it should index, the canonical tag helps to consolidate ranking signals - in other words, the canonical version will get the benefit of inbound links to the other versions.
Continuing with my example, I used Rank Math to specify 'SEO' as the primary category for my post. This added the following canonical tag to both versions:
<link rel="canonical" href="https://www.technicallyproduct.co.uk/seo/spike-in-direct-traffic-it-could-be-google-discover/" />
You can see this by going to the post and viewing the page source (right-click and 'View page source', or Ctrl+U in Chrome):
Bear in mind that Google takes a canonical tag as a "strong signal", but may choose to ignore it if it has reasons to do so. (For example, Google prefers to canonicalize HTTPS pages over equivalent HTTP pages.)
Another way to mitigate the problem of duplicate content caused by placing WordPress posts in multiple categories is to use redirects. In other words, you redirect all the non-canonical URLs to your one, chosen, canonical version. Google states that this is also a "strong signal" and actually stacks with the canonical tag:
"Keep in mind that these methods can stack and thus become more effective when combined. This means that when you use two or more of the methods, that will increase the chance of your preferred canonical URL appearing in search results."
Using redirects has other benefits, for example all traffic will show up under a single URL in your analytics tool such as Google Analytics 4 (GA4).
Now you could add your redirects in one at a time using a WordPress plugin such as Redirection, but this would be very time consuming should you often put posts in multiple categories. If you've used Rank Math or Yoast SEO to specify a primary category, there's a quicker way to do it.
It involves using a WordPress function created by the lovely people at Happy Prime. It's the final code snippet on the following page: https://happyprime.co/2021/10/14/using-a-primary-category-in-wp-permalinks/
You will need to make one change to the snippet, swapping '_primary_category' to either '_yoast_wpseo_primary_category' (for Yoast) or 'rank_math_primary_category' (for Rank Math). Here's what the code looks like for Rank Math, with the amended line highlighted:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | add_action( 'template_redirect', 'redirect_to_primary_category' ); function redirect_to_primary_category() { global $post; // If the post is single. if ( is_single( $post->ID ) ) { // If the post has multiple categories assigned. $cats = wp_get_post_categories( $post->ID ); if ( count( $cats ) > 1 ) { // Get the primary category. $primary_category = get_post_meta( $post->ID, 'rank_math_primary_category', true ); // If the primary category isn't in the URL, redirect. global $wp; $primary_object = get_term( $primary_category ); if ( 0 !== strpos( $wp->request, $primary_object->slug ) ) { $primary_url = home_url( '/' . $primary_object->slug . '/' . $post->post_name . '/' ); wp_safe_redirect( $primary_url, 301 ); exit; } } } } |
If you know what you're doing, you could add the function to your site by editing your theme's functions.php file - but the easier and safer option is to use a WordPress plugin such as Code Snippets.
This lets you add the snippet and set it to 'only run on site front end' - which means it will definitely not break anything in the admin area. The function will continue to run even if you change theme, but on the other hand can easily be disabled using a toggle in the Code Snippets plugin.
Here's what it looks like in Code Snippets:
I put this in place on my site, and now the URL https://www.technicallyproduct.co.uk/analytics/spike-in-direct-traffic-it-could-be-google-discover/ automatically redirects to the canonical version based on my chosen primary category, https://www.technicallyproduct.co.uk/seo/spike-in-direct-traffic-it-could-be-google-discover/
The website Redirect Checker shows it is a permanent (301) redirect:
Very helpful, thank you. Can you tell me if adding the redirection code snippet (not a problem for me as I maintain a custom plugin for such things) is necessary if we've been using RankMath for a long time (longer than most of the content we've posted for the last 2-3 years)? I understand the benefit of the redirections and can see where older content might need canonicals set, but wondering if it's needed for anything posted since setting up RankMath (a great plugin, by the way - I've used both Yoast and AIO in the past, and find RM… Read more »
Hi Trisha - I reckon you don't need to add the redirection code unless you have a specific reason for doing so. For example, if you are getting a lot of traffic to non-canonical URLs which is making your analytics messy, or if Google is ignoring your canonical tag and indexing the wrong URLs (rare).
In most cases, using Rank Math to specify a primary category and set the canonical should be more than adequate from an SEO point of view! This was as much a thought exercise/technical challenge as much as anything.
Thank you! I'll pay closer attention to what my analytics are telling me, but good to know it may not be necessary.