How to Programmatically Set SEO Titles and Descriptions for Archive Pages in WordPress
Managing SEO metadata like titles and descriptions is a critical part of optimizing your WordPress site for search engines. While plugins like Yoast SEO offer user-friendly interfaces for this, advanced implementations—especially in multisite or custom post type setups—can benefit significantly from programmatic control.
In this article, we’ll walk through how to programmatically manage SEO titles and descriptions for archive pages using WordPress filters, improve consistency across your network, and redirect unused archive pages to avoid SEO duplication.
Why Programmatic SEO Metadata?
While plugin settings are convenient, they fall short in some advanced use cases. Programmatic SEO control offers several advantages:
- Consistency across multisite environments
- Automation for dynamic post types and taxonomies
- Avoiding duplication through redirection
- Fine-grained control over content-specific logic
This is especially important when dealing with archive pages like custom taxonomies (make
, model
, bodytype
) or post types (used-car
, dealership
), where plugin settings can’t dynamically adjust to the context.
Understanding Archive Page Types
In WordPress, archive pages include:
- Default Archives: Category, tag, author, date archives
- Custom Post Type Archives: e.g.,
/used-car/
- Custom Taxonomy Archives: e.g.,
/make/volkswagen/
,/model/polo/
These pages need tailored metadata to rank well and avoid generic or duplicate SEO content.
Setting SEO Titles and Descriptions Using Filters
Yoast SEO provides WordPress filters to modify metadata programmatically:
wpseo_title
wpseo_metadesc
wpseo_opengraph_title
wpseo_opengraph_desc
Here’s an example of how to hook into these filters to dynamically control titles and descriptions for different archive types:
Title Filter Example
add_filter('wpseo_title', 'wect_filter_title_example');
add_filter('wpseo_opengraph_title', 'wect_filter_title_example');
function wect_filter_title_example($title) {
if (is_tax('brand')) {
$term = get_queried_object();
$title = 'Browse ' . $term->name . ' Listings • ' . get_bloginfo();
}
return $title;
}
Description Filter Example
add_filter('wpseo_metadesc', 'wect_filter_description_example');
add_filter('wpseo_opengraph_desc', 'wect_filter_description_example');
function wect_filter_description_example($description) {
if (is_tax('brand')) {
$term = get_queried_object();
$description = 'Discover all ' . $term->name . ' cars for sale at ' . get_bloginfo() . '.' }
return $description;
}
You can apply this pattern for other taxonomies like make
, bodytype
, and even custom post types, if redirection is not used.
Avoiding SEO Duplication with Archive Redirection
In many WordPress setups, default archive pages (like /used-car/
) are automatically created for custom post types. However, custom-built landing pages (e.g., /used-cars-for-sale/
) often serve as the primary destination for users and search engines.
To maintain SEO consistency and simplify site management, it’s a common practice to redirect default archive URLs to these curated landing pages.
Why This Matters:
- To avoid maintaining separate SEO metadata for both the archive and the main landing page, which reduces complexity.
- To ensure consistent SEO strategy and content structure, preventing duplicate content signals to search engines.
Here’s how you can implement this logic in WordPress:
$queried_object = get_queried_object();
$post_type = get_post_type();
if (is_a($queried_object, 'WP_Post_Type')) {
if ($post_type == 'used-car') {
$used_cars_page = get_the_permalink(get_field('used_cars_page', 'options'));
if (!empty($used_cars_page)) {
wp_redirect($used_cars_page, 301);
exit;
}
}
// Repeat for other post types that requires redirection.
}
Best Practices for Managing SEO in Code
- Use ACF or custom functions to fetch dynamic data (e.g., dealership city, model name, redirect pages)
- Fallback safely in every function to prevent empty metadata
- Test across all site types in a multisite setup
- Avoid over-optimization: keep titles natural and readable
- Standardization: keep titles on a site standardized (e.g., If
|
or•
is used in titles, all titles on that site should follow this structure) - Use 301 redirects only for archive pages you fully replace with landing pages
Compatibility with SEO Plugins
These techniques are compatible with Yoast SEO and Rank Math, as both rely on WordPress filters. However:
- If using Rank Math, you must use their equivalent filters (
rank_math/frontend/title
,rank_math/frontend/description
) - Avoid setting conflicting values in the plugin’s settings panel
Performance Considerations and Pitfalls
- Too many complex conditionals can slow down page loads
- Always cache metadata-heavy logic if it’s reused
- Avoid unnecessary database lookups for every archive page
Before & After Example
Before:
Title: Archives – Cars
Description: Just another WordPress site.
After:
Title: 2021 VW Polo Vivo Trendline • Used Car for Sale
Description: R189,900 for a used 2021 VW Polo Vivo with 65,000km. Finance available. Inspected and ready to drive.
Conclusion
Controlling SEO titles and descriptions programmatically gives you precision and flexibility, especially in large or complex WordPress setups. With a thoughtful implementation, you can significantly improve search performance while reducing plugin reliance and administrative overhead.