Yoast SEO title and description in site search results

Yoast SEO lets you customize the meta title and description for your articles, which is used in Google search results and browser tabs.

Since these are often more descriptive than the actual page title, I recommend using them in your on-site search results as well. (I also recommend using SearchWP for improved site search).

Yoast SEO stores this information as post metadata, using the key _yoast_wpseo_title for the title and _yoast_wpseo_metadesc for the description.

But don’t output this directly – the titles often include dynamic variables like %%title%% for the post title.

You can use wpseo_replace_vars( $string, $post ) to replace the variables with actual text.

You should also provide a fallback in case Yoast SEO is no longer active, or no custom title was provided.

Template tags for title and excerpt

Include the functions below in your theme’s functions.php file or a core functionality plugin.

You can then use be_search_entry_title() in place of get_the_title(), and be_search_entry_excerpt() in place of get_the_excerpt() in your theme, wherever it defines how posts appear in search results.

/**
 * Entry title in site search
 * @link https://www.billerickson.net/yoast-seo-title-and-description-in-site-search-results/
 *
 */
function be_search_entry_title() {
	$title = '';
	if( function_exists( 'wpseo_replace_vars' ) ) {
		global $post;
		$title = get_post_meta( get_the_ID(), '_yoast_wpseo_title', true );
		$title = wpseo_replace_vars( $title, $post );
	}
	if( empty( $title ) ) {
		$title = get_the_title();
	}
	return $title;
}

/**
 * Entry excerpt in site search
 * @link https://www.billerickson.net/yoast-seo-title-and-description-in-site-search-results/
 *
 */
function be_search_entry_excerpt() {
	$excerpt = '';
	if( function_exists( 'wpseo_replace_vars' ) ) {
		global $post;
		$excerpt = get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true );
		$excerpt = wpseo_replace_vars( $excerpt, $post );
	}
	if( empty( $excerpt ) ) {
		$excerpt = get_the_excerpt();
	}
	return $excerpt;
}

If you’re using one of my starter themes, you’ll use these functions in /partials/archive-search.php.

If you’re using a StudioPress theme, also add this to your functions.php file so Genesis uses our custom functions:

/*
 * Genesis, use search entry title
 * @link https://www.billerickson.net/yoast-seo-title-and-description-in-site-search-results/
 *
 */
function be_genesis_search_entry_title( $title ) {
	if( is_search() )
		$title = be_search_entry_title();
	return $title;
}
add_filter( 'genesis_post_title_text', 'be_genesis_search_entry_title');

/**
 * Genesis, use search entry excerpt
 * @link https://www.billerickson.net/yoast-seo-title-and-description-in-site-search-results/
 *
 */
function be_genesis_search_entry_excerpt() {
	if( ! is_search() )
		return;

	remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
	echo wpautop( be_search_entry_excerpt() );
}
add_action( 'genesis_entry_content', 'be_genesis_search_entry_excerpt', 9 );

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Leave A Reply