How to Get Yoast’s Primary Category

Displaying WordPress categories in a theme can be a challenge because a post could belong to several different categories and if the theme has to display 30 categories for each post in a list, it could get messy. This is where a primary category comes in handy.

Often I find myself needing to find the most important category for a blog post when a site was only designed to show one or a client likes to add 15 different categories.

Yoast SEO is a great plugin for managing general SEO for WordPress, but it also provides a handy feature that can easily be overlooked. If multiple categories have been selected for a blog post, it will designate the first selected category as the “Primary Category”.

Since Yoast handles selecting this primary category, we can hook in and grab it without too much trouble. Here’s a basic layout of what this function will do for us:

/**
 * Gets the primary category set by Yoast SEO.
 *
 * @return array The category name, slug, and URL.
 */
function get_primary_category( $post = 0 ) {
	if ( ! $post ) {
		$post = get_the_ID();
	}
	
	// Get the categories for the post.
	$category = get_the_category( $post );

	// If post has a category assigned.
	if ($category){
		if ( class_exists('WPSEO_Primary_Term') )
		{
			// Show the post's 'Primary' category, if this Yoast feature is available, & one is set

			if (is_wp_error($term)) {
				// Default to first category (not Yoast) if an error is returned
			} else {
				// Yoast Primary category goodness
			}
		}
		else {
			// Default, display the first category in WP's list of assigned categories
	}
	return $primary_category;
}

First, we check if a $post ID has been passed (if we ever need to find the primary category of a post outside of the loop). If no ID was passed then we need to get the current post’s ID since we can assume we’re inside the loop.

if ( ! $post ) {
	$post = get_the_ID();
}

Next, we’ll use get_the_category() to retrieve all the current posts categories. We also set up an empty array that we’ll use to store the primary category’s information.

$category = get_the_category( $post );
$primary_category = array();

Once we have all the categories currently assigned to the post, we’ll check to see if there’s only one listed because we can skip this whole process if there’s only one category assigned.

// If the post has more than one category assigned to it.
if ( 1 > count($category) ){
  // Set up the necessary variables.
  $category_display = '';
  $category_slug = '';
  $category_link = '';
  
  // Grab Yoast's reference to the 'Primary Category'.
  
}

Make sure the Yoast plugin is installed and if the WPSEO_Primary_Term class doesn’t exist,  just use the first category in the list.

It’s not the best fallback option because WordPress displays the categories in alphabetical order. So it wouldn’t serve as the best primary category, but it serves our purpose for now.

if ( class_exists('WPSEO_Primary_Term') ) {
  
	// Use the class to grab the primary category.
	
}
else {
	// Display the first category in WordPress' list of assigned categories
	$category_display = $category[0]->name;
	$category_slug = $category[0]->slug;
	$category_link = get_category_link( $category[0]->term_id );
}

Finally we’ll grab the category using the WPSEO_Primary_Term class:

// Get the primary category for the current post.
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', $post );
$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;
  $category_link = get_category_link( $category[0]->term_id );
} else {
  // Yoast Primary category goodness
  $category_display = $term->name;
  $category_slug = $term->slug;
  $category_link = get_category_link( $term->term_id );
}

Now that we have the primary category, we can have the function return or echo it out depending on how it is used in our blog.

Here’s the whole function if you just want to paste this into your current project:

3 thoughts on “How to Get Yoast’s Primary Category”

  1. Hello!
    You have made a little mistake in your code:

    $wpseo_primary_term = new WPSEO_Primary_Term( ‘category’, get_the_id( $post ) );

    It will be return with false, because you previously get the post ID, so you don’t need to pass it to get_the_id()…

    This is the correct code:

    $wpseo_primary_term = new WPSEO_Primary_Term( ‘category’, $post );

    Cheers

Leave a Reply