WordPress does not return custom post meta in the REST API by default. To read or write a meta field such as the Rank Math SEO title, description or focus keyword through /wp-json/wp/v2/posts, register it with register_post_meta() and show_in_rest. A small mu-plugin does the job in a few lines. This guide shows how to do it securely: editors can edit the fields, and anonymous visitors cannot see them.
The problem: custom post meta is missing from the REST API
If you publish or audit posts with a script (an AI agent, a migration tool, a CI job), you will soon want to check the SEO fields too. Request a post in edit context and look at meta:
curl -s -u "editor:APP_PASSWORD" \
"https://example.com/wp-json/wp/v2/posts/123?context=edit&_fields=meta"
You get the meta keys of your theme and other plugins, but no rank_math_* keys. Rank Math stores them as normal post meta, but it does not register them for the REST API. Its own rankmath/v1/updateMeta endpoint can write them, but there is no endpoint to read them back. Without the Headless CMS option, you cannot verify what was written.
Prerequisites
- A WordPress site with access to
wp-content/mu-plugins/(SSH, SFTP or your hosting panel). - A user with the Editor role (or higher) and an Application Password for the REST API calls. If only some users should have Application Passwords, see How to Restrict WordPress Application Passwords to Specific Users.
- HTTPS. WordPress accepts Application Passwords only over HTTPS, unless
WP_ENVIRONMENT_TYPEislocal.
Tested with WordPress 7.1.5, PHP 8.3.6, MariaDB 10.11 and WP-CLI 2.12.0 on a local test site, and with Rank Math SEO on a production site.
Step 1: Create the mu-plugin
Must-use plugins load automatically and cannot be deactivated from the admin, which suits small site-wide fixes like this one. Create the folder if it does not exist:
mkdir -p wp-content/mu-plugins
Create wp-content/mu-plugins/rest-rank-math-meta.php:
<?php
/**
* Plugin Name: REST Rank Math meta
* Description: Expose Rank Math SEO fields of posts in the WordPress REST API (edit context, editors only).
*/
add_action( 'init', function () {
$keys = array( 'rank_math_title', 'rank_math_description', 'rank_math_focus_keyword', 'rank_math_canonical_url' );
foreach ( $keys as $key ) {
register_post_meta( 'post', $key, array(
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'sanitize_callback' => 'rank_math_canonical_url' === $key ? 'esc_url_raw' : 'sanitize_text_field',
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
) );
}
} );
// Hide the fields from public (view) responses: return them only with ?context=edit.
add_filter( 'rest_prepare_post', function ( $response, $post, $request ) {
if ( 'edit' !== $request['context'] ) {
$data = $response->get_data();
foreach ( array_keys( $data['meta'] ?? array() ) as $key ) {
if ( str_starts_with( $key, 'rank_math_' ) ) {
unset( $data['meta'][ $key ] );
}
}
$response->set_data( $data );
}
return $response;
}, 10, 3 );
What each part does:
register_post_meta()withshow_in_rest => trueadds the four keys to themetaobject of the posts endpoint, for reading and writing.auth_callbackallows a write only to users who can edit that specific post. An Author cannot change the SEO fields of an Editor’s post.sanitize_callbackstrips HTML from the text fields and cleans the canonical URL.- The
rest_prepare_postfilter removes the fields from every response that is not ineditcontext. Without it, the focus keyword of every published post would be visible to anyone at/wp-json/wp/v2/posts.
Check the syntax before you upload it:
php -l wp-content/mu-plugins/rest-rank-math-meta.php
No syntax errors detected in wp-content/mu-plugins/rest-rank-math-meta.php
For other plugins, change the list of keys. For a custom post type, replace 'post' with its name and hook the filter to rest_prepare_{post_type}.
Step 2: Write the fields through the REST API
Send the fields inside meta when you update a post:
curl -s -u "editor:APP_PASSWORD" -X POST \
-H "Content-Type: application/json" \
-d '{"meta":{"rank_math_title":"My SEO title","rank_math_description":"Short description","rank_math_focus_keyword":"rest api meta"}}' \
"https://example.com/wp-json/wp/v2/posts/123?context=edit&_fields=id,meta.rank_math_title,meta.rank_math_focus_keyword"
{"id":123,"meta":{"rank_math_title":"My SEO title","rank_math_focus_keyword":"rest api meta"}}
The same meta object also works when you create a post (POST /wp/v2/posts). You no longer need a second call to a plugin-specific endpoint.
Verification
Run three checks: one as an editor in edit context, one as an anonymous visitor, and one as an Author on someone else’s post.
# 1. Editor, edit context: the fields are returned
curl -s -u "editor:APP_PASSWORD" \
"https://example.com/wp-json/wp/v2/posts/123?context=edit&_fields=meta.rank_math_title"
{"meta":{"rank_math_title":"My SEO title"}}
# 2. Anonymous, published post: no rank_math_* keys
curl -s "https://example.com/wp-json/wp/v2/posts/123?_fields=meta"
{"meta":{"footnotes":""}}
# 3. Author on a post of another user: refused
curl -s -u "author:APP_PASSWORD" -X POST -H "Content-Type: application/json" \
-d '{"meta":{"rank_math_title":"x"}}' "https://example.com/wp-json/wp/v2/posts/123"
{"code":"rest_cannot_edit","message":"Sorry, you are not allowed to edit this post.","data":{"status":403}}
You can also confirm the value in the database with WP-CLI:
wp post meta get 123 rank_math_title
Troubleshooting
- 401
rest_forbidden_contextorrest_cannot_editfor a valid editor: the Application Password was not accepted. Check that the site runs over HTTPS (or thatWP_ENVIRONMENT_TYPEislocalon a test site), and that your web server passes theAuthorizationheader to PHP. - The keys do not appear at all: request
context=edit. With the filter of Step 1, the fields are hidden in the defaultviewcontext, even for editors. - The keys appear but are empty: the post has no value yet. Rank Math then uses its default title and description templates.
- HTML disappears from the description: that is
sanitize_text_fieldworking as intended. SEO meta must be plain text.
Conclusion
Registering post meta with register_post_meta() and show_in_rest is the cleanest way to read and write plugin fields through the WordPress REST API. Keep two safeguards: an auth_callback that checks edit_post, and a filter that hides the fields from public responses. For more core functions you will use in plugins like this, see Most Commonly Used WordPress Functions.
References
Entrepreneur | Full-stack developer | Founder of MediSign Ltd. I have over 15 years of professional experience designing and developing web applications. I am also very experienced in managing (web) projects.
