Get primary category

#5413
  • Resolved Jason01
    Rank Math free

    Hi Everyone,
    Grateful if you could please help, i would like to call the primary category through Rankmath.

    Previously I was with yoast i was the using the below code

    $primary_cat_id=get_post_meta($product->id,'_yoast_wpseo_primary_product_cat',true);
    if($primary_cat_id){ $product_cat = get_term($primary_cat_id, 'product_cat');
    echo $product_cat->name;
    }

    The code still work for old product but for new one it is not working.

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

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

    Would you mind sharing the complete code so that I can advise on how to achieve this in RM? Are you using the primary category on titles or descriptions or this code is used to display custom replacement variables?

    Looking forward to helping you. Thank you.

    ​​​​​​​

    $primary_cat_id=get_post_meta($product->id,'_yoast_wpseo_primary_product_cat',true);
    if($primary_cat_id){ $product_cat = get_term($primary_cat_id, 'product_cat');
    
    $markup['brand']   =  $product_cat->name; }

    Here the full code

    I have being using this in the class-wc-structured-data.php to get the Brand Name as my primary category is the brand

    • This reply was modified 4 years, 11 months ago by Jason01.

    Hello,

    Please use the following code to get the primary category:

    
    $primary_cat_id = get_post_meta( $product->id, 'rank_math_primary_category', true );
    if ( $primary_cat_id ) {
        $product_cat = get_term( $primary_cat_id, 'product_cat' );
        $markup['brand'] = $product_cat->name;
    }
    

    Hope that helps. Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Get primary category’ is closed to new replies.