12

The WordPress SEO plugin by Yoast allows users to add SEO titles and meta descriptions to taxonomy term archive pages. These are then used in the head of the document.

I´m trying to display the SEO title of taxonomy terms as an H1 in my taxonomy archive template.

To do this in a post, post type or page is easy:

echo get_post_meta($post->ID, '_yoast_wpseo_title', true); 

On archive templates this doesn´t work.

Does anyone know how to get this to work?

Screenshot example

This is the title of a specific tag term. I´m trying to display this title - but then for a custom taxonomy term - in my archive templates.

The tag archive edit screen

Final code

This is what I ended up using in my archive.php template. It works if you´re using a custom taxonomy. For tags or categories have a look at Mike Madern´s answer below.

<h1 class="archive-title">
<?php
if ( is_tax() ) :                                                                   
    $taxonomy = get_queried_object()->taxonomy;
    $term_id = get_queried_object()->term_id;
    $meta   = get_option( 'wpseo_taxonomy_meta' );
    $title  = $meta[$taxonomy][$term_id]['wpseo_title'];
    //printf( '<pre>%s</pre>', print_r( get_option( 'wpseo_taxonomy_meta' ), 1 ) );
    if ( isset($meta) && !empty($title) ) :         
        echo apply_filters( 'the_title', $title );
    else :
        single_term_title();    
    endif;
endif;                              
?>
</h1>  
16

Get Archive SEO titles

If you defined a Custom Post Type archive title you can get that by:

$titles = get_option( 'wpseo_titles' );
$title  = $titles['title-ptarchive-POST_TYPE'];

echo apply_filters( 'the_title', $title );

Remember to replace POST_TYPE by your own Custom Post Type.

To display all the wpseo_title variables, you can use:

printf( '<pre>%s</pre>', print_r( get_option( 'wpseo_titles' ), 1 ) );

So you can easily pick the one you need.

Get Term SEO titles

Categories

By using this code you can get the SEO title you defined:

$cat_id = get_query_var( 'cat' );
$meta   = get_option( 'wpseo_taxonomy_meta' );

$title  = $meta['category'][$cat_id]['wpseo_title'];

echo apply_filters( 'the_title', $title );

Tags

By using this code you can get the SEO title you defined:

$tag_id = get_query_var( 'tag' );
$meta   = get_option( 'wpseo_taxonomy_meta' );

$title  = $meta['post_tag'][$tag_id]['wpseo_title'];

echo apply_filters( 'the_title', $title );

To display all wpseo_taxonomy_meta variables, you can use:

printf( '<pre>%s</pre>', print_r( get_option( 'wpseo_taxonomy_meta' ), 1 ) );

This way you can see the structure and available variables.

  • You added the title in SEO > titles & metas > Post Types? – Mike Madern Mar 7 '13 at 11:08
  • I improved my answer with the solution :-) – Mike Madern Mar 7 '13 at 11:51
  • I'm guessing you missed my last post edit :-P I added a version with $tag_id. – Mike Madern Mar 7 '13 at 12:11
  • 1
    Thanks so much for the code, I´d never be able to figure this out. I made a few minor changes to get it to work with custom taxonomies. – Mattvic Mar 7 '13 at 12:45
  • This is great. I had to use get_queried_object()->term_id, instead of get_query_var() though, as the former returns the id, the latter returns the slug, making the code throw “undefined index” notices. Also, take care to check if there is anything set at $meta['post_tag|category'], as you'll get undefined index notices as well. (WP 4.2.2, btw) – cabgfx May 20 '15 at 10:48
5

On archive page in post loop add following line of code to make it work

echo get_post_meta(get_the_ID(), '_yoast_wpseo_title', true); 

Tell me whether it is working for you or i will provide another solution.

  • That didn´t work unfortunately. It shows the SEO title of the first post in the archive. I´ll probably have to dive into the DB to see where this data is stored. – Mattvic Mar 7 '13 at 10:22
  • Are you using it in loop? Can you show me the code? – Vinod Dalvi Mar 7 '13 at 10:28
  • I´ve added my code. Not sure if I explained that I wanted to output the page title, not the titles of each post in the loop. – Mattvic Mar 7 '13 at 10:45
  • This is exactly what I was looking for. Outputs the SEO title in the loop. Also, I replaced get_the_ID() with $post->ID – gerbz Aug 8 '15 at 2:06
  • this saves my life. good.. – Mukii kumar Jan 5 '18 at 7:52
0

Getting the focus keyword for categories and tags

To get the focus keyword of a Category or Tag programmatically, you can use either of these two methods:

            /** Method 1. Tap into the options directly. **/
            $meta = get_option( 'wpseo_taxonomy_meta' ); // needed only once, so keep this outside/before your foreach loop.

            $posttags = get_the_tags();
            if ($posttags) {
                foreach( $posttags as $tag ) {
                    echo $meta['post_tag'][$tag->term_id]['wpseo_focuskw'];
                }
            }

This is basically the approach used by Mike at the end of his post.

or

            /** Method 2. Use Yoast's function. **/
            $posttags = get_the_tags();
            if ($posttags) {
                foreach( $posttags as $tag ) {
                    echo WPSEO_Taxonomy_Meta::get_term_meta( $tag->term_id, 'post_tag', 'focuskw' );
                }
            }

I tend to prefer the second approach because this way I don't have to fetch or care about getting the meta option myself.

In your functions.php, you may want to test for if class_exists( 'WPSEO_Taxonomy_Meta' ) and then maybe wrap the Yoast function into a functon of your own, to be safe in case you ever were to stop using Yoast SEO down the line.

Some background

As mentioned in the original question, something like get_post_meta( $tag->term_id, '_yoast_wpseo_focuskw', true); won't work for categories and pages (it will return nothing.)

So why is that?

That's because, for categories and tags, the focus keyword is saved elsewhere in the database. Instead of being saved inside of the wp_postmeta table as usual, it's buried deep inside of the wp_options table, as a JSON object.

The methods above let us access these values.


Finally, if you're like me and can't remember the name of taxonomies (e.g.: it's not obvious why tags use taxonomy post_tag and not just tag, just check out the URLs when you edit a category or a tag: it'll show …taxonomy=category… or the like.

0

Reply Getting the focus keyword for categories and tags

// it work thankyou
// with rest api

get /wp-json/wp/v2/categories?slug=slug_name

plugin.in use Method 1

change line

$meta['post_tag'][$tag->term_id]['wpseo_focuskw'];

$meta['category'][$param_post_id]['wpseo_focuskw'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.