Get Primary Term if Yoast SEO is being Used

Get the primary term for the current post if using Yoast SEO. If not, fallback to first selected category.

<?php
$currentID = get_the_ID();
$category = get_the_category();

// Get primary (Yoast) term if it is set
$category_display = '';
$category_slug = '';

if ( class_exists('WPSEO_Primary_Term') ) {

     // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
	$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
	$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
	$term = get_term( $wpseo_primary_term );

     if ( is_wp_error( $term ) ) {

          // Default to first category (not Yoast) if an error is returned
          $category_display = $category[0]->name;
          $category_slug = $category[0]->slug;

     } else {

          // Set variables for category_display & category_slug based on Primary Yoast Term
          $category_id = $term->term_id;
          $category_term = get_category($category_id);
          $category_display = $term->name;
          $category_slug = $term->slug;

     }

} else {

     // Default, display the first category in WP's list of assigned categories
     $category_display = $category[0]->name;
     $category_slug = $category[0]->slug;

}

Use this modified version if you’d like to force the primary category to be a top-level category. So if the primary category is a sub category, this will set the parent of that sub category as the term.

<?php
$currentID = get_the_ID();
$category = get_the_category();

// Get primary (Yoast) term if it is set
$category_display = '';
$category_slug = '';

if ( class_exists('WPSEO_Primary_Term') ) {

     // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
	$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
	$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
	$term = get_term( $wpseo_primary_term );

     if ( is_wp_error( $term ) ) {

     	// Default to first category (not Yoast) if an error is returned
		$category_display = $category[0]->name;
          $category_slug = $category[0]->slug;

     } else {

          // Check if category has parent
          $category_id = $term->term_id;
          $category_term = get_category($category_id);

          // if primary category is a child
          if( $category_term->category_parent > 0 ) {

               // Get parent category object
               $parent = $category_term->parent;
               $parent_object = get_category($parent);

               // Set parent category variables
               $category_display = $parent_object->name;
               $category_slug = $parent_object->slug;

          // if primary cateogry is a parent
          } else {

			// Yoast Primary category
			$category_display = $term->name;
               $category_slug = $term->slug;

          }

     }
} else {

	// Default, display the first category in WP's list of assigned categories
	$category_display = $category[0]->name;
     $category_slug = $category[0]->slug;

}

About Matt Whiteley

Matt has been building bespoke WordPress websites for over 10 years specializing in the Genesis Framework. He integrates Advanced Custom Fields heavily in all builds, especially with the new Gutenberg Block Editor, allowing clients to easily modify all aspects of the their website with ease.

He works with a wide variety of small businesses and agencies across the country providing development services as well as hosting and maintenance. When he isn't coding he enjoys spending time with his wife and two children, golfing and playing poker.