Get Primary taxonomy |

I’m trying to retrieve ONLY the primary term for a custom taxonomy.

There is a primary ‘building_type’ selected, and I’d like to get ONLY the primary one, instead of all the terms checked. Here’s the code I used to get ALL the terms (which works).
$termsP = the_terms( get_the_ID(), array( //'building_type', 'lease_phase', 'topic', 'building_stakeholder') );

And here’s what I’m using to try and get the primary building_type:

$post_id = get_the_ID();											

$primaryTerms = the_seo_framework()->get_custom_field( '_primary_term_' . 'building_type', $post_id ); 

I’ve also tried this which also doesn’t work:

$taxonomy = 'building_type'; 													

$post_id = get_the_ID();													

$terms = wp_get_post_terms($post_id, $taxonomy, ['fields' => 'all']);													

$primary_term = intval(get_post_meta( $post_id, '_primary_term_' . $taxonomy, true ));													

foreach($terms as $term) {													   

if( $primary_term == $term->term_id ) {													        // this is a primary category														   }													}													

return $primary_term;

Any help is greatly appreciated.

  • This topic was modified 3 hours, 25 minutes ago by jinhabarefoot.

The page I need help with: [log in to see the link]


This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Your Advertisement Here