Yoast SEO pulls each post’s meta description from the editor metabox, but when you run thousands of auto-generated pages or a custom post type fed by an API, typing one by hand is not an option. You set it in code with Yoast’s wpseo_metadesc filter. This DevAnswers guide covers the global override, conditional logic, custom fields, and the duplicate-tag gotcha.
The wpseo_metadesc Filter (Quick Answer)
Yoast builds the description tag through its Meta_Description_Presenter and runs the text through the wpseo_metadesc filter before printing it to the <head>. Hook a callback that returns your string and Yoast uses that instead. Add the code to a child theme’s functions.php or a code snippets plugin (see where to add code in WordPress); a syntax error in functions.php can white-screen the site, so a snippets plugin is the safer home.
add_filter( 'wpseo_metadesc', function ( $description ) {
return 'Shop hand-knit Aran wool sweaters, made in Galway and shipped worldwide.';
} );
That replaces the description on every page. You rarely want a single description site-wide, but it shows the core mechanism: return a string, and Yoast renders it in place of its own.
Override Only Specific Pages (Conditional Logic)
In practice you change the description on certain pages and leave the rest to Yoast. Check the context with a conditional tag such as is_singular() or get_post_type(), and here is the part people get wrong: return the incoming $description unchanged for everything you do not target. Returning an empty string there would blank the description on every other page of the site.
add_filter( 'wpseo_metadesc', function ( $description ) {
if ( is_singular( 'product' ) ) {
$year = date( 'Y' );
return 'In stock now for ' . $year . '. Free delivery across Ireland on orders over EUR 50.';
}
return $description; // leave every other page to Yoast
} );
is_singular( 'product' ) returns true only when a single product post is being displayed, so the override is scoped to that one post type. Pass a different slug, or swap in is_category(), is_page(), or a get_post_type() check, to target another context.
Pull the Description From a Custom Field
Directory and listing sites usually store a summary in a custom field, and you can serve that as the description so every entry gets its own without anyone opening the metabox. With Advanced Custom Fields, read it with get_field(), and guard for an empty value so posts without a summary fall back to Yoast’s default.
add_filter( 'wpseo_metadesc', function ( $description ) {
if ( is_singular( 'listing' ) ) {
$summary = get_field( 'seo_summary' );
if ( $summary ) {
return wp_strip_all_tags( $summary );
}
}
return $description;
} );
wp_strip_all_tags() keeps stray markup out of the tag. Not using ACF? Swap get_field() for core post meta: get_post_meta( get_the_ID(), 'seo_summary', true ).
Override the Title and Social Description Too
Yoast manages every head tag through a presenter you can modify or extend, not just the description, so the same callback pattern covers the rest. Use wpseo_title to filter the SEO title tag and wpseo_opengraph_desc to filter the Open Graph (social share) description. If a missing or duplicated title tag is your real problem, see our guide on the WordPress title tag in the header.
add_filter( 'wpseo_title', function ( $title ) {
if ( is_singular( 'product' ) ) {
return get_the_title() . ' | DevAnswers Store';
}
return $title;
} );
Common Gotchas: Duplicate Tags, Length, and Caching
Two description tags in your source. Some themes output their own meta description tag, so you will see two <meta name="description"> tags in the page source if yours does. View the source, search for <meta name="description", and if you find two, look in the theme’s SEO options or header.php (ideally via a child theme), since many themes add the excerpt as a description tag by default. Disabling the theme’s own meta output is required to avoid two description tags.
Length. Search results only show a limited slice of the description, so the common recommendation is to keep desktop descriptions to roughly 155 to 160 characters; trim long programmatic strings with mb_substr(). Caching. If you run a caching plugin or a page cache, clear it after adding the code, then reload the page source to confirm the new description.
Summary
The wpseo_metadesc filter turns Yoast’s description into something you control in code. Return your own string, scope it with a conditional tag, and always return the incoming $description for pages you are not targeting. Pull the text from a custom field for listing sites, reach for wpseo_title and wpseo_opengraph_desc when you need the title or social tags too, and watch for a duplicate description tag left behind by your theme.
Comments