Get Yoast SEO title for custom taxonomy – WordPress

I’m trying to get the SEO title from a custom taxonomy.

Here’s my current code for it:

$my_yoast_wpseo_title = get_term_meta( $term_id, '_wpseo_title', true );
if( $my_yoast_wpseo_title ){
    echo $my_yoast_wpseo_title;
} else {
    echo 'No title';
}

It doesn’t work.

So I tried different meta keys like _yoast_wpseo_title and everything I could find in their docs and other snippets.

Nothing works.

So I checked the complete output of get_term_meta. Like this:

$my_yoast_wpseo_title = get_term_meta( $term_id );
print_r($my_yoast_wpseo_title);

It shows a lot of meta fields. But the Yoast meta isn’t stored there?! Is their any other place?

The taxonomy has a custom SEO title and shows it in the frontend.

Best Answer

The Yoast SEO Titles for Terms are stored in the options table.

This will at least give you the idea you’re after.

[php]$options = get_option( ‘wpseo_taxonomy_meta’ );
// $options will be an array of taxonomies where the taxonomy name is the array key.
$term_id = get_queried_object_id();
foreach ( $options[‘your_term_here’] as $id => $option ) {
if ( $term_id === $id ) {
/* This returns an array with the following keys
‘wpseo_title’
‘wpseo_focuskw’
‘wpseo_linkdex’
*/
echo ( ! empty( $option[‘wpseo_title’] ) ) ? $option[‘wpseo_title’] : ‘no title’;
}
}[/php]

Leave a Comment