How to Programmatically Set Yoast SEO Fields via the WordPress REST API
Content
Many developers and site administrators use the WordPress REST API to automate content publishing. A common challenge encountered is getting Yoast SEO metadata, such as the SEO title, meta description, and focus keyphrase, to save and display correctly when creating or updating posts programmatically. This guide explains why this happens and provides the most effective solutions.
The Core Problem: Indexables and the REST API
When Yoast SEO fields are updated via the standard WordPress REST API, the metadata (e.g., _yoast_wpseo_title) is often saved to the database. You can confirm this by checking the wp_postmeta table. However, the SEO analysis indicators in the WordPress admin may remain gray, and, more critically, the correct metadata may not appear on the frontend of your site.
This discrepancy occurs because Yoast SEO uses a separate, optimized database structure called 'indexables' to manage and serve SEO data quickly. Simply saving the post meta is not always sufficient to trigger a rebuild of these indexable records. The process that updates these tables often needs to be explicitly initiated after an API update.
Common Solutions
1. Enable Yoast Meta for the REST API
By default, Yoast's custom fields are not exposed in the WordPress REST API. To make them available, you must explicitly register them. This is typically done by adding the following code to your theme's functions.php file or a custom plugin.
function habilitar_yoast_meta_para_api() {
register_post_meta('post', '_yoast_wpseo_metadesc', [
'show_in_rest' => true,
'single' => true,
'type' => 'string'
]);
register_post_meta('post', '_yoast_wpseo_focuskw', [
'show_in_rest' => true,
'single' => true,
'type' => 'string'
]);
register_post_meta('post', '_yoast_wpseo_title', [
'show_in_rest' => true,
'single' => true,
'type' => 'string'
]);
}
add_action('init', 'habilitar_yoast_meta_para_api');
Note: Replace 'post' with your custom post type slug if you are not working with standard posts.
2. Manually Trigger Indexable Updates
After your API call successfully saves the post and its Yoast metadata, you may need to manually prompt Yoast SEO to reprocess the data and update its indexables table. A reliable method is to use the official Yoast Test Helper plugin.
This plugin provides a 'Reset indexables tables & migrations' tool. While this is a manual solution in the admin dashboard, its functionality can be triggered programmatically after an API update by calling the underlying functions that this tool uses.
3. Structure Your API Payload Correctly
Ensure your POST or PUT request to the WordPress REST API is structured correctly. The Yoast meta fields should be nested within the meta object of your payload.
{
"title": "Your Post Title",
"content": "Your post content.",
"status": "publish",
"meta": {
"_yoast_wpseo_title": "Your SEO Title",
"_yoast_wpseo_metadesc": "Your meta description.",
"_yoast_wpseo_focuskw": "primary keyword"
}
}
Important Considerations and Limitations
- Taxonomy Terms: The issue of metadata not appearing on the frontend until a manual save occurs is particularly prevalent with taxonomy terms (like product categories or tags) updated via API. The same principle applies—the indexables for these terms need to be rebuilt.
- Beyond Standard Fields: Setting more complex Yoast SEO data, such as programmatically inserting a Table of Contents block or populating social media metadata, often requires a deeper integration using Yoast SEO's specific developer APIs and filters, which are documented on their developer portal.
- Custom Post Types: For forms that submit to custom post types, the post type must be public and have
show_in_restset totruefor the Yoast SEO metabox and its data to be accessible via the API.
By understanding the role of indexables and using the methods above, you can successfully integrate Yoast SEO management into your automated publishing workflows.
Related Support Threads Support
-
Insert article field in seo titlehttps://wordpress.org/support/topic/insert-article-field-in-seo-title/
-
Meta Titles and Descriptions Not Displayed After Sync via REST APIhttps://wordpress.org/support/topic/meta-titles-and-descriptions-not-displayed-after-sync-via-rest-api/
-
Description of mediahttps://wordpress.org/support/topic/description-of-media/
-
Request for automatic implementation of the Spotify icon in Yoast social mediahttps://wordpress.org/support/topic/request-for-automatic-implementation-of-the-spotify-icon-in-yoast-social-media/
-
populate yoast fields when adding data using REST APIhttps://wordpress.org/support/topic/populate-yoast-fields-when-adding-data-using-rest-api/
-
API Support for Adding SEO Metadata and Yoast Blocks Programmaticallyhttps://wordpress.org/support/topic/api-support-for-adding-seo-metadata-and-yoast-blocks-programmatically/
-
Yoast SEO Metabox – Additional Buttonhttps://wordpress.org/support/topic/yoast-seo-metabox-additional-button/
-
Automatically extract the image and title and populate the respective social andhttps://wordpress.org/support/topic/automatically-extract-the-image-and-title-and-populate-the-respective-social-and/
-
Add metadata when posting with WP API RESThttps://wordpress.org/support/topic/add-metadata-when-posting-with-wp-api-rest/
-
Yoast SEO Fieldshttps://wordpress.org/support/topic/yoast-seo-fields-3/
-
autofill default focus keyphrasehttps://wordpress.org/support/topic/autofill-default-focus-keyphrase/
-
Meta variables for Zapier update?https://wordpress.org/support/topic/meta-variables-for-zapier-update/
-
Setting Yoast SEO fields via API for automated WordPress publishinghttps://wordpress.org/support/topic/setting-yoast-seo-fields-via-api-for-automated-wordpress-publishing/
-
Forum and topic can’t be set as Social Media Posting from Yoast Settingshttps://wordpress.org/support/topic/forum-and-topic-cant-be-set-as-social-media-posting-from-yoast-settings/