Skip to content

Instantly share code, notes, and snippets.

@matbrady
Last active April 1, 2020 17:01
Show Gist options
  • Save matbrady/ca06415ead35ae1977c81fbe13b0fdb5 to your computer and use it in GitHub Desktop.
Save matbrady/ca06415ead35ae1977c81fbe13b0fdb5 to your computer and use it in GitHub Desktop.
Get Primary Taxonomy Term set by Yoast SEO

This have been modified to gracefully fail when used on Post types that do not support the described taxonomy.

/**
* Returns the primary term for the chosen taxonomy set by Yoast SEO
* or the first term selected.
*
* @link https://www.tannerrecord.com/how-to-get-yoasts-primary-category/
* @param integer $post The post id.
* @param string $taxonomy The taxonomy to query. Defaults to category.
* @return array The term with keys of 'title', 'slug', and 'url'.
*/
function get_primary_taxonomy_term($post = 0, $taxonomy = 'category')
{
if (!$post) {
$post = get_the_ID();
}
$post_type = get_post_type($post);
$taxonomies = get_object_taxonomies($post_type);
// FAIL if the post does not support the taxonomy
if (!in_array($taxonomy, $taxonomies)) {
return false;
}
$terms = get_the_terms($post, $taxonomy);
$primary_term = array();
if ($terms) {
$term_display = '';
$term_slug = '';
$term_link = '';
if (class_exists('\WPSEO_Primary_Term')) {
$wpseo_primary_term = new \WPSEO_Primary_Term($taxonomy, $post);
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term($wpseo_primary_term);
if (is_wp_error($term)) {
$term_display = $terms[0]->name;
$term_slug = $terms[0]->slug;
$term_link = get_term_link($terms[0]->term_id);
} else {
$term_display = $term->name;
$term_slug = $term->slug;
$term_link = get_term_link($term->term_id);
}
} else {
$term_display = $terms[0]->name;
$term_slug = $terms[0]->slug;
$term_link = get_term_link($terms[0]->term_id);
}
$primary_term['url'] = $term_link;
$primary_term['slug'] = $term_slug;
$primary_term['title'] = $term_display;
}
return $primary_term;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment