One of the features of the Rank Math SEO plugin is the ability to mark a term like category or product category as primary.
See How to Choose a Primary Category.
This Pro tutorial shows how to create a custom PHP function that takes either ‘name’ or ‘link’ as its first argument and the taxonomy name as the second argument and returns the primary term’s name or link for the specified taxonomy.
The function can then be used in Bricks editor on single templates or in query loops on archives with its echo dynamic tag to print the primary category/product category/custom taxonomy term’s name or link or linked name.
For example, when editing a specific WooCommerce product:

Here, we marked “Coat” as the primary category of this product.
After implementing the tutorial, output on the front end:

with “Coat” linking to
https://example.com/product-category/coat/
Step 1
Install and activate Rank Math. The ability to designate a term as primary is available in the free version.
Edit your posts (of any post type) and mark a primary term as needed.
Step 2
Add the following in child theme‘s functions.php or a code snippets plugin:
<?php
// $data_type can be 'name' or 'link'
function bl_get_primary_term_data( string $data_type = 'name', string $taxonomy = 'category' ): string {
$data = '';
$term_id = get_post_meta( get_the_ID(), "rank_math_primary_{$taxonomy}", true );
switch ( $data_type ) {
case 'name':
$term = get_term_by( 'id', $term_id, $taxonomy );
$data = $term->name;
break;
case 'link':
$data = esc_url( get_term_link( (int) $term_id, $taxonomy ) );
break;
}
return $data;
}
We are setting name and category as the defaults.
Meaning, to get the primary category’s name you could simply use the function in Bricks like this:
{echo:bl_get_primary_term_data}
and to get the primary category’s link:
{echo:bl_get_primary_term_data(link)}
Step 3
In the Bricks editor, to get/output the name of your primary term:
{echo:bl_get_primary_term_data(name,product_cat)}
Replace product_cat with the name of your taxonomy.
To get/output the link of your primary term:
{echo:bl_get_primary_term_data(link,product_cat)}
Replace product_cat with the name of your taxonomy.
To output primary product category linking to its term archive URL:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Primary category: <strong><a href=»{echo:bl_get_primary_term_data(link,product_cat)}»>{echo:bl_get_primary_term_data(name,product_cat)}</a></strong> |
