Last active
November 20, 2018 12:19
-
-
Save brendonexus/391ac32813122d943e4deb040c1a5d69 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