• Resolved jinhabarefoot

    (@jinhabarefoot)


    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 years, 8 months ago by jinhabarefoot.

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

Viewing 1 replies (of 1 total)
  • Thread Starter jinhabarefoot

    (@jinhabarefoot)

    Took a while but was able to get this to work:

    
    if (has_term('', 'building_type')){
    									
    
    $primary_building_type = the_seo_framework()->get_primary_term( get_the_ID(), 'building_type' );
    														
    
    $building_type_link = get_term_link($primary_building_type);
    													
    
    ?>,
    													
    
    <a href="<?php echo $building_type_link; ?>"><?php echo $primary_building_type->name; ?></a>, 
    													
    													
    
    <?php 
    												
    
    }
    												
    
    else{
    
    												}
Viewing 1 replies (of 1 total)

The topic ‘Get Primary taxonomy’ is closed to new replies.