Skip to content

Instantly share code, notes, and snippets.

@mrl22
Forked from brendonexus/function.php
Created November 20, 2018 12:19
Show Gist options
  • Save mrl22/ad85de5cad296f669292c2a4d636c4ed to your computer and use it in GitHub Desktop.
Save mrl22/ad85de5cad296f669292c2a4d636c4ed to your computer and use it in GitHub Desktop.
Get WordPress Primary Category
// get primary category - must have Yoast SEO installed
function get_primary_category($post_id){
$term_list = wp_get_post_terms($post_id, 'category', ['fields' => 'all']);
foreach($term_list as $term) {
if( get_post_meta($post_id, '_yoast_wpseo_primary_category',true) == $term->term_id ) {
return $term;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment