Aleš Sýkora / January 9, 2024 / 4 comments

How to get RankMath primary WooCommerce product category

Post summary: Learn how to retrieve and display the primary category of a product in WooCommerce using the RankMath plugin. WooCommerce lacks a built-in feature to assign a primary category, but RankMath fills this gap. With the provided code snippet, you can retrieve the primary category ID and output the category link and name. Improve your SEO…

WooCommerce is a popular e-commerce platform for WordPress that allows users to create and manage online stores. RankMath is a widely used SEO plugin for WordPress, offering various features to optimize websites for search engines. One of the valuable functionalities of RankMath is the ability to set a primary category for each product or post in WordPress. In this article, we will explore how to get the primary product category set by RankMath with PHP.

By default, WooCommerce does not provide a built-in way to designate a primary category for products. However, RankMath extends the functionality of WooCommerce. The primary category can be beneficial for SEO purposes and organizing products in a structured manner. To utilize this feature effectively, it is necessary to understand how to get and display the primary category on product pages with PHP.

Code snippet for WordPress Integration:

To retrieve the primary category assigned to the product in WooCommerce using the RankMath plugin, you can utilize the following code snippet:

<?php
    $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_product_cat', true );
    $category_id = $primary_cat_id; // Replace with the category ID

    // Retrieve category object
    $category = get_term($category_id, 'product_cat');

    if (!empty($category)) {
        $category_link = get_term_link($category);
        $category_name = $category->name;

        // Output the category link and name
        echo '<a href="' . $category_link . '"><strong>' . $category_name . '</strong></a>';
    } else {
        // Handle the case when no primary category is set
    }
?>

The code above retrieves the primary category ID using the get_post_meta() function with the meta key 'rank_math_primary_product_cat' (you can use rank_math_primary_cat key for post categories). It then assigns the ID to the variable $category_id. After that, the code retrieves the category object using the get_term() function by passing the category ID and the taxonomy slug 'product_cat' (change to category if you want to use it for posts). If a valid category is found, it obtains the category link and name, and outputs them as an HTML link.

Shortcode integration:

To create a shortcode from the code provided, follow the steps below:

  1. Open the functions.php file of your WordPress theme or create a custom plugin.
  2. Add the following code to define the shortcode:
function retrieve_primary_category_shortcode() {
    ob_start();
    
    $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_product_cat', true );
    $category_id = $primary_cat_id; // Replace with the category ID

    // Retrieve category object
    $category = get_term($category_id, 'product_cat');

    if (!empty($category)) {
        $category_link = get_term_link($category);
        $category_name = $category->name;

        // Output the category link and name
        echo '<a class="link--base" href="' . $category_link . '"><strong>' . $category_name . '</strong></a>';
    } else {
        // Handle the case when no primary category is set
    }
    
    return ob_get_clean();
}
add_shortcode( 'primary_category', 'retrieve_primary_category_shortcode' );
  1. Save the file or plugin and upload it to your WordPress site.

You can now use the [primary_category] shortcode in your WordPress editor or any text widget to display the primary category of a product on product page (!). When the shortcode is being processed, it will retrieve the Rank Math’s primary product category and output the category link and name.

Example usage: [primary_category]

This shortcode provides a convenient way to display the primary category without directly modifying the template files.

Conclusion:

With the RankMath plugin, WooCommerce users can take advantage of the primary category feature to enhance their SEO efforts and organize their products effectively. By utilizing the provided code snippet, you can retrieve the primary category of a product and display it on the product pages of your WooCommerce store.

This allows visitors to easily navigate through main categories of products and improves the overall user experience.

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

4 comments

  • I am trying to display the primary category of my products in a product loop in my product archive pages. Do I just implement the code snippet and then insert the shortcode in my loop element?

    • A

      Hello Ben, if you use the default WooCommerce product loop, then you need to hook in the loop. For example, you can use the woocommerce_after_shop_loop_item_title. Then you can add the PHP code or the shortcode. Do you know how to use the hooks?

      • I’m rebuilding my site with Bricks Builder. I am using the query loop on a block element to display my products in product archive page in a grid layout. I found that there already is a parameter in Bricks Builder that pulls the primary category set with RankMath but it only returns the ID instead of the term/category name. Is there a way to return the term value instead for this?

        Either that or I’m trying out the code snippet that you have provided above and insert the shortcode via the shortcode element positioned inside my query loop block.

        • A

          Hello Ben, I didn’t know about that. However, I will stick with mine solution. If you use the Block with query, then you should be able to put Code block with the PHP function or ─ if you need to use it sitewide on more places, then add the code through code snippets and use the shortcode.

Share Your Thoughts