How to Update Yoast Keyword and Meta Description using the WordPress REST API

By default, the WordPress REST API does not include support for updating the Yoast metadata of a post. However, with a simple update to your functions.php file, extending the existing WordPress REST API endpoint (POST /wp/v2/posts/) to support updating Yoast SEO meta fields is possible. Instead of registering a new custom route, you can use the register_rest_field function to add custom fields to the default post endpoint.

Here’s how you can add Yoast SEO meta fields (_yoast_wpseo_metadesc and _yoast_wpseo_focuskw) to the update POST /wp/v2/posts/ endpoint:

Edit your functions.php file in WordPress.

Add this code to the bottom:

/**
 * Add REST API support for Yoast Meta 
 -------------------------------------------------------------------------*/
function register_yoast_meta_in_rest() {
    register_rest_field('post', 'yoast_description', array(
        'get_callback'    => function($post) {
            return get_post_meta($post['id'], '_yoast_wpseo_metadesc', true);
        },
        'update_callback' => function($value, $post) {
            update_post_meta($post->ID, '_yoast_wpseo_metadesc', sanitize_text_field($value));
        },
        'schema' => array(
            'type'        => 'string',
            'description' => 'Meta description for Yoast SEO',
        ),
    ));

    register_rest_field('post', 'yoast_keyword', array(
        'get_callback'    => function($post) {
            return get_post_meta($post['id'], '_yoast_wpseo_focuskw', true);
        },
        'update_callback' => function($value, $post) {
            update_post_meta($post->ID, '_yoast_wpseo_focuskw', sanitize_text_field($value));
        },
        'schema' => array(
            'type'        => 'string',
            'description' => 'Meta keywords for Yoast SEO',
        ),
    ));
}

How This Works

This adds the yoast_keyword and yoast_description fields to /wp/v2/posts/ so you can update the Yoast SEO meta data via API.

It allows updating the two most important fields via WordPress’ native REST API method: POST /wp/v2/posts/<id>, making it possible to modify Yoast SEO metadata when updating a post.

Example API Request

Here is what to put in the Body of your POST API request if you’re using a tool like Make or n8n:

{
"yoast_description": "Your meta description",
"yoast_keyword": "Your keyword"
}

Here is what the full request looks like:

$ curl -X POST https://example.com/wp-json/wp/v2/posts/123 -d '{"yoast_description": "Your meta description", "yoast_keyword": "Your keyword"}'

This approach ensures Yoast SEO meta fields are integrated seamlessly with the core WordPress REST API, rather than requiring a separate endpoint!

Enjoyed this post? Subscribe to my weekly newsletter!

Leave a Comment