• Hi! My articles that are published within 2 or more categories are getting multiple URLs and Google Search Console is having trouble indexing.

    Let’s say I made an article within this categories: “Fashion”, “Entertainment” and “News”. News is the main category, so the URL would be:
    https:/website.com/news/article-slug

    But it still works if I use the other categories:

    https:/mywebsite.com/fashion/article-slug -> Would get me to the same article, but with a different URL.

    So the article ends up having as many links as categories. What I would like to happen is this:

    https:/mywebsite.com/entertainment/article-slug -> redirects to the URL with the main category -> https:/mywebsite.com/news/article-slug

    It would be fantastic if any other category would get me to that URL based on the article’s slug. Is this possible?

    My permalinks are currently like this:

    https://website.com/%category%/%postname%/
    Category: .
    Tags: tags

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Here’s how you can adjust the function to specifically redirect to the “News” category URL when an article belongs to multiple categories but is accessed through a non-“News” category URL:

    function redirect_to_news_category() {
        if (is_single() && !is_admin()) {
            global $post;
            // Define the main category slug
            $main_category_slug = 'news';
            // Get current category slug from URL
            $current_cat = get_query_var('category_name');
            
            // Check if the current category is not the main category and the post belongs to the main category
            if ($current_cat && $current_cat !== $main_category_slug) {
                // Check if the post belongs to the main category
                if (has_category($main_category_slug, $post->ID)) {
                    // Build the redirect URL
                    $redirect_url = site_url() . '/' . $main_category_slug . '/' . $post->post_name;
                    wp_redirect($redirect_url, 301); // Permanent redirect to the main category URL
                    exit;
                }
            }
        }
    }
    add_action('template_redirect', 'redirect_to_news_category');
    

    What This Code Does:

    • It checks if the current request is for a single post and not within the admin dashboard.
    • It defines “news” as the main category slug and checks the current URL for a different category slug.
    • If the accessed category is not “news” but the post belongs to the “news” category, it redirects to the URL that uses “news” as the category slug.
    • This redirection is done using a 301 HTTP status code, indicating a permanent redirect, which is beneficial for SEO as it passes link equity to the redirected URL.

    Deployment:

    • Add this code to your theme’s functions.php file or a site-specific plugin.
    • Replace 'news' with the actual slug of your main category if it’s different.
    • Test this on a staging site first to ensure it works as expected without impacting other functionalities or SEO negatively.

    Important Considerations:

    • Caching & Performance: If you’re using caching plugins or server-side caching, ensure they’re configured to handle redirects properly to avoid redirect loops or caching the wrong responses.
    • SEO Monitoring: After implementing redirects, keep an eye on Google Search Console for any increase in 404 errors or other crawl issues.
    • Backup: Always back up your website before making changes to the code.

    This custom solution should address your specific requirement to redirect articles accessed through non-main category URLs to the main category URL, consolidating your SEO efforts and reducing duplicate content issues.

    Thread Starter alexcastor

    (@alexcastor)

    Thanks, but I don’t understand where should I put this code? And “news” was just an example. Is Chatgpt accurate when it comes to WordPress stuff?

    You would add the code to your functions.php file. Change the “news” slug to match your main category slug.
    ChatGPT is accurate if you ask it correctly and at times it does require some redirection. I use it quite often in my field of work.

    You would add the code to your functions.php file. Change the “news” slug to match your main category slug.

    I’m no developer.

    But I think the point OP was trying to make was that the solution should be generalized to work for all situations where multiple categories are selected for any post, irrespective of the primary category involved.

    The “news” category given is just ONE scenario. There may be other scenarios where the primary category may be different. So the advice to change the news slug to match the main/primary category slug does not solve the problem, unless you meant the entire block of code should be duplicated for every additional scenario.

    UPDATE

    I used “primary category” a couple of times above. But it seems WordPress has no such notion, making it impossible to programmatically tell WordPress which of the multiple categories selected should be the “primary” one (I’m referring to the general sense, not statically assigning one category as the “main” category as in the above code).

    Luckily OP is using the Yoast SEO plugin which has just such a feature:https://yoast.com/help/how-to-select-a-primary-category/

    SO, perhaps, this Yoast SEO function could be leveraged somehow to implement this (again, I’m not a developer so can’t help at all.)

    • This reply was modified 8 months, 2 weeks ago by George Appiah.
    Thread Starter alexcastor

    (@alexcastor)

    I think the point OP was trying to make was that the solution should be generalized to work for all situations where multiple categories are selected for any post, irrespective of the primary category involved.

    You’re on point! The problem is that multiple valid URL are being created and it’s messing with my Google Search Console links.

    I used “primary category” a couple of times above. But it seems WordPress has no such notion

    Actually, when I’m picking the categories for my post it lets me select one as the main category. And when I press “view post” after publishing, WordPress takes me to the URL with the main article in it. The problem is it can also be accessed replacing the category in the URL with any other category picked for the article.

    If possible, I would like it to work like Dexerto. In the following example, you can swap “youtube” with any other category and it will redirect you to the main ULR with youtube in it:

    https://www.dexerto.com/youtube/mrbeast-urges-man-to-change-his-name-to-avoid-getting-sued-2580890/

    Thank for the additional information and hopefully I understood everything correctly. Since you are using Yoast SEO we will leverage its ability to assign a primary category for each post and have any additional categories and their URL’s redirect to it.
    Here is the updated code:

    function redirect_to_yoast_primary_category() {
        if (is_single() && !is_admin() && class_exists('WPSEO_Primary_Term')) {
            global $post;
            
            // Instantiate WPSEO_Primary_Term with 'category' taxonomy
            $wpseo_primary_term = new WPSEO_Primary_Term('category', $post->ID);
            $primary_term_id = $wpseo_primary_term->get_primary_term();
            
            if ($primary_term_id && !is_wp_error($primary_term_id)) {
                $primary_category = get_term($primary_term_id, 'category');
                
                // Verify that get_term did not return an error and returned a valid WP_Term object
                if (!is_wp_error($primary_category) && $primary_category instanceof WP_Term) {
                    $primary_category_slug = $primary_category->slug;
                    $current_cat_slug = get_query_var('category_name');
                    
                    // Proceed with redirection only if the current category slug is valid and not the primary category
                    if (!empty($current_cat_slug) && $current_cat_slug !== $primary_category_slug) {
                        $redirect_url = get_permalink($post->ID);
                        
                        // Ensure the redirect URL is a valid URL to avoid redirection to unexpected locations
                        if (false !== filter_var($redirect_url, FILTER_VALIDATE_URL)) {
                            wp_redirect($redirect_url, 301); // Permanent redirect with HTTP status code 301
                            exit;
                        }
                    }
                }
            }
        }
    }
    add_action('template_redirect', 'redirect_to_yoast_primary_category');
    

    Actually, when I’m picking the categories for my post it lets me select one as the main category.Actually, when I’m picking the categories for my post it lets me select one as the main category.

    That fooled me as well. It’s NOT a native WordPress feature: it’s the Yoast SEO plugin feature I mentioned. If you disable Yoast, this option will be gone.

    Thread Starter alexcastor

    (@alexcastor)

    Here is the updated code:

    That code didn’t work, it broke all my URLs saying I got “too many redirections”.

    If you disable Yoast, this option will be gone.

    You’re right! I just installed WPCode. I’m trying to find a snippet code that does what I need. Hopefully there is one somewhere!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Posts have multiple URL’ is closed to new replies.