Back to Community

How to Programmatically Set Yoast SEO Fields via the WordPress REST API

14 threads Sep 7, 2025 PluginYoast seo

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_rest set to true for 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