-
-
Save mrl22/ad85de5cad296f669292c2a4d636c4ed to your computer and use it in GitHub Desktop.
Get WordPress Primary Category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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