Skip to content
main logo
  • Blog
  • LinkedIn
  • GitHub
  • Recipes
Home » Blog » Make WordPress Post Meta Available in REST API

Make WordPress Post Meta Available in REST API

Posted by Ryan Neilson on November 22, 2023November 22, 2023 in PHP, WordPress

When registering a new post meta field in WordPress we can make that field available via the REST API. While this doesn’t happen by default, it’s fairly simple to do.

Registering a Post Meta Field

The code to register a new post meta typically looks something like this:

<?php

register_post_meta( 'post', 'example_post_meta_field', [
  'auth_callback' => function() {
    return current_user_can( 'edit_posts' );
  },
  'sanitize_callback' => 'sanitize_text_field',
  'show_in_rest' => true,
  'single' => true,
  'type' => string
]);

The code above registers a new post meta field example_post_meta_field on posts of type ‘post’. The line ‘show_in_rest’ => true indicates this new post meta field should be made available via the REST API (but only for users who have permissions to ‘edit_posts’).

Usage with Custom Post Types

If you’re planning to use custom post meta on a custom post type, there is an additional consideration when registering your custom post type. In the register_post_type function call, you must include the ‘supports’ array within the args array. The ‘supports’ array must include ‘custom-fields’ in order for any custom post meta values applied to that post type to also appear via the REST API.

Example CPT Registration

<?php

register_post_type( 'books',
  array(
    'labels' => array(
      'name' => __( 'Books' ),
      'singular_name' => __( 'Book' )
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'books'),
    'show_in_rest' => true,
    'supports' => array(
      'title', 
      'editor', 
      'author',
      'custom-fields'
    ) 
  )
);

In the code above we register a ‘books’ post type, making sure to set ‘show_in_rest’ => true and making sure to include ‘custom-fields’ in the ‘supports’ array.

Let’s say we have our ‘books’ post type and we want to register a custom post meta value ‘publisher’. The code might look something like this:

<?php

register_post_meta( 'books', 'publisher', [
  'auth_callback' => function() {
    return current_user_can( 'edit_posts' );
  },
  'sanitize_callback' => 'sanitize_text_field',
  'show_in_rest' => true,
  'single' => true,
  'type' => string
]);

The ‘publisher’ post meta will now be accessible via the REST API for posts of the ‘books’ type.

Share this article:
Tweet
Share
the author profile picture

Ryan Neilson is a software developer from Nova Scotia, Canada, where he lives with his wife and their menagerie of pets. He has worked extensively with PHP, WordPress, JavaScript, React, SCSS and CSS. Ryan can be found professionally at News Corp, where he works as a lead software engineer.

linkedingithub

Related Posts

Add WordPress User to Multisite via WP CLI

Adding a new user to a sub-site of a WordPress multisite is a two step process via the WP CLI. Add a user to the network First we’ll add a new user to the multisite network with the following WP CLI command: In this example we’ve assigned the role of administrator, while the other options …

Continue reading “Add WordPress User to Multisite via WP CLI”

Change Autocompleter Triggers in the WordPress Block Editor

We can use filters to change the autocompleter triggers in the WordPress block editor. By ‘triggers’, I mean the typed character combination we use to make an autocompleter appear. For example, by default we can use @ to populate a list of site users. We can also use [[ to select a post link from …

Continue reading “Change Autocompleter Triggers in the WordPress Block Editor”

© 2025 Neilson Web Design