Cray
Cray

Reputation: 5483

WordPress: Get Yoast SEO title for custom taxonomy

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.

Upvotes: 0

Views: 3702

Answers (3)

vitinnn46
vitinnn46

Reputation: 21

$term = get_queried_object(); 
$options = get_option( 'wpseo_taxonomy_meta' );
echo $options[$term->taxonomy][$term->term_id]['wpseo_title']; //Meta Title
echo $options[$term->taxonomy][$term->term_id]['wpseo_title']; //Meta Description

This is another solution to get the meta title and meta description out of yoast seo in the taxonomy. Paste this code into archive.php or taxonomy.php of your theme.

Upvotes: 1

Cray
Cray

Reputation: 5483

I found another way to do this:

$my_yoast_wpseo_title = WPSEO_Taxonomy_Meta::get_term_meta($id, 'produkt_vendor', 'title');
if( $my_yoast_wpseo_title ){
    echo $my_yoast_wpseo_title;
} else {
    echo 'No title';
}

You can used that with title and desc without the prefix wpseo_

Upvotes: 1

Howard E
Howard E

Reputation: 5649

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

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

$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';
    }
}

Upvotes: 1

Related Questions