Your newly created post might have a few terms attached to it. Terms are items in a custom taxonomy.
Say, you need to organize movies into a few genres such as horror, action, adventure, and such. In this case, the custom post type (CPT) should be Movies, the taxonomy is the genre, and the terms will be horror, action, and adventure.
Now, when querying a couple of CPTs on a page — you might want to only display the primary term in each CPT. It’s also good to have a primary category for breadcrumbs and SEO purposes. And we are going to learn how to do exactly that with Bricks Builder and some custom code.
Bear in mind, that the primary category isn’t natively available in WordPress. Only SEO plugins like Yoast and Rank Math have this feature. At this point, I assume you already have created a CPT, taxonomy, and at least 2 terms.
Firstly, place this piece of code into your child theme, snippet plugin (I recommend Fluent Snippets), or a custom plugin.
Rank Math
function av_get_rm_primary_term( $taxonomy = 'category' ) {
$post_id = get_the_ID();
$term_arr = array();
// Rank Math
if( class_exists( '\RankMath\Common' ) ) {
$rm_primary_term = get_post_meta( $post_id, 'rank_math_primary_category');
if( !empty($rm_primary_term) ) {
$rm_primary_term = get_term($rm_primary_term[0]);
if( !is_wp_error( $rm_primary_term ) && !empty( $rm_primary_term ) && '' != $rm_primary_term ) {
$term_arr['name'] = esc_html( $rm_primary_term->name );
$term_arr['link'] = esc_url( get_term_link( $rm_primary_term, $taxonomy ) );
return $term_arr;
}
}
}
// Default
$terms = get_the_terms( $post_id, $taxonomy );
if( empty($term_arr) && !empty( $terms ) ) {
$term_arr['name'] = esc_html( $terms[0]->name );
$term_arr['link'] = esc_url( get_term_link( $terms[0]->term_id, $taxonomy ) );
return $term_arr;
}
return false;
}
Code language: PHP (php)
Yoast
function av_get_yoast_primary_term( $taxonomy = 'category' ) {
$post_id = get_the_ID();
$term_arr = array();
// Yoast
if( class_exists( 'WPSEO_Primary_Term' ) ) {
$yoast_primary_term = new WPSEO_Primary_Term( $taxonomy, $post_id );
$yoast_primary_term = get_term( $yoast_primary_term->get_primary_term() );
if( !is_wp_error( $yoast_primary_term ) && !empty( $yoast_primary_term ) && '' != $yoast_primary_term ) {
$term_arr['name'] = esc_html( $yoast_primary_term->name );
$term_arr['link'] = esc_url( get_term_link( $yoast_primary_term, $taxonomy ) );
return $term_arr;
}
}
// Default
$terms = get_the_terms( $post_id, $taxonomy );
if( empty($term_arr) && !empty( $terms ) ) {
$term_arr['name'] = esc_html( $terms[0]->name );
$term_arr['link'] = esc_url( get_term_link( $terms[0]->term_id, $taxonomy ) );
return $term_arr;
}
return false;
}
Code language: PHP (php)
Default
function av_get_default_primary_term( $taxonomy = 'category' ) {
$post_id = get_the_ID();
$term_arr = array();
// Default
$terms = get_the_terms( $post_id, $taxonomy );
if( empty($term_arr) && !empty( $terms ) ) {
$term_arr['name'] = esc_html( $terms[0]->name );
$term_arr['link'] = esc_url( get_term_link( $terms[0]->term_id, $taxonomy ) );
return $term_arr;
}
return false;
}
Code language: PHP (php)
Remember to whitelist the function name in Bricks by putting it into the bricks/code/echo_function_names filter.
Add a new post, publish it, and associate it with the terms you have created before using the sidebar in the editor. Below the list of terms, you should see a dropdown that allows you to select a primary taxonomy.
Choose any one of them and hit save. It should look like this.
Now head over to Bricks; open a page or template for your CPT archive and add some elements. This is how my structure panel looks like:
Great work so far. Now to get the primary term for the post; Bricks provides a very handy function for that.
{echo:av_get_rm_primary_term('your_taxonomy'):array_value|name}
Code language: Bash (bash)
For more info, check out the official Bricks documentation.
To get the link, it’s essentially the same thing, but by replacing name with link of course. Like so:
{echo:av_get_rm_primary_term('your_taxonomy'):array_value|link}
Code language: Bash (bash)
Replace av_get_rm_primary_term with your function name and your_taxonomy with your custom post type taxonomy.
Some styling added:
That’s it! Happy coding.
PS: If you want the primary category from other SEO plugins, let me know in the comments below and I’ll add them to this post.