Issues with primary category + URL structure after update

#939432
  • Resolved Keywordjäger
    Rank Math free

    Dear support,

    we have an issue which directly correlates with SEO and site / URL structure, which occurred after updating RankMath.

    We run a recipe site which has multiple categories.

    If you like to understand the issue better, you can visit https://rezepte-mix.de

    Before the initial update, RankMath correctly selected the “primary” category on every recipe, which was always “rezepte”. Every recipe we had, could be found under rezepte-mix.de/rezepte/name-of-recipe to keep the URL-structure simple and clean, as we run a blog on the site in parallel. /blog/name-of-article

    After the update, the whole site structure broke and every recipe which had multiple sub-categories shows this in the URL now, although the “primary” category “rezepte” is still activated.

    Before: rezepte-mix.de/rezepte/name-of-recipe/
    Now: rezepte-mix.de/rezepte/desserts/name-of-recipe/ (this is wrong)

    Yes, every recipe does have the primary and a sub-category, but the “primary term” doesn’t work anymore for whatever reason. The problem is that all of our recipes are indexed now under the wrong URL structure, which is a pity.

    Unfortunately, I couldn’t resolve the issue myself. This is not a caching-issue.

    The permalink-structure on WordPress is also correct: /%category%/%postname%/

    The site runs on the “Kadence Theme”, however, I couldn’t find someone on their support-site who experienced the same issue.

    Thank you for any help in advance and a happy new 2025!

    PS: I even tried forcing the “primary category in URL” running a code-snippet using PHP but it didn’t work too. Here is the code snippet I used:

    ————–
    function force_primary_category_in_permalink( $permalink, $post ) {
    if ( strpos( $permalink, ‘%category%’ ) === false ) {
    return $permalink;
    }

    $categories = get_the_category( $post->ID );

    if ( $categories ) {

    $primary_category_id = get_post_meta( $post->ID, ‘rank_math_primary_category’, true );

    if ( $primary_category_id ) {

    $primary_category = get_category( $primary_category_id );
    $main_category = $primary_category ?: $categories[0];
    } else {

    $main_category = $categories[0];
    }

    $permalink = str_replace( ‘%category%’, $main_category->slug, $permalink );
    }

    return $permalink;
    }
    add_filter( ‘post_link’, ‘force_primary_category_in_permalink’, 10, 2 );

    ————–


    Kind regards
    Alexey

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.

    We’ve recently seen some issues with the primary category in the URL. The issue will be fixed in the next update. For now, you can replace the code on line 251 of the wp-content/plugins/seo-by-rank-math/includes/class-common.php with the following code and see if that works for you:

    if ( empty( $terms ) || ! in_array( absint( $primary ), $terms, true ) ) {
    

    Let us know how it goes. Looking forward to helping you.

    Hey Rakibuzzaman Siam,

    thank you so much for the code! It worked!

    Much appreciated!

    Alexey

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Issues with primary category + URL structure after update’ is closed to new replies.