skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    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_


  2. 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';
        }
    }
    
    Login or Signup to reply.
  3. $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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search