Skip to content

Instantly share code, notes, and snippets.

@sybrew
Created March 20, 2024 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sybrew/8864ee7743121715b72697f41b62e68a to your computer and use it in GitHub Desktop.
Save sybrew/8864ee7743121715b72697f41b62e68a to your computer and use it in GitHub Desktop.
Gets primary term name automatically from any post type for The SEO Framework WordPress plugin.
<?php
// Don't include the PHP opening tag if PHP is already open.
function get_primary_term_name( $post_id ) {
$taxonomies = array_keys( array_filter(
tsf()->taxonomy()->get_hierarchical( 'objects', get_post_type( $post_id ) ),
'is_taxonomy_viewable',
) );
$taxonomy = reset( $taxonomies );
return $taxonomy
? ( tsf()->data()->plugin()->post()->get_primary_term( $post_id, $taxonomy )->name ?? '' )
: '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment