Monthly Archives: February 2015

Last updated by at .

Replace Post Excerpts with the Yoast META Description

Someone sent a question through this site to me yesterday.

Hi Mark,
Thanks for your site. I’ve been using yoast meta descriptions since beginning my site (800 posts) and now want to use the post excerpt in archives. Is there a way to use the yoast meta desc. as the wordpress excerpt? I find documentation on using excerpts as meta, but not the other way around.
Thanks for your help,

If you’re wondering Yoast SEO is the most popular WordPress SEO Plugin and it allows you to override a lot of default WordPress post values to better optimise your on-page SEO. If you’re wondering what a META description is then you should read this article on Wikipedia.

As it turns out, the solution isn’t too hard. First, check the WordPress documentation to see if there’s a filter that can be used when the post excerpt is retrieved (there is). Second, have a poke around the wp_postmeta MySQL database table and check what meta_key Yoast uses to store his post META description. Turns out that key is _yoast_wpseo_metadesc. Once we know that it’s simply a matter of writing a couple of lines of PHP and dropping it into the functions.php file:

add_filter( 'get_the_excerpt', 'replace_post_excerpt_filter' );

function replace_post_excerpt_filter($output)
{
        $output=get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true);
        return $output;
}

In the first line we’re adding the get_the_excerpt filter and in the third adding the new filter procedure. The procedure itself is fairly simply, the get_post_meta function allows us to pull the Yoast META description for the post. Then we’re replacing the excerpt passed to the function with the Yoast value.

Of course this is going to replace the post excerpt everywhere. If you just wanted to change the value in your WordPress archives then you could just edit your child theme’s archive.php file (assuming your theme has an archive.php file). Just look for the main loop and some code that looks something like the_excerpt(); and replace it with:

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