• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Support the WordPress REST API in a Custom Post Type

Discover in this tutorial how to support the WordPress REST API in a Custom Post Type (CPT) using code.

The nice thing about creating your own Custom Post Type is that you have full control over it.

For example?

If you want it to support the WordPress REST API just add the argument 'show_in_rest' => true, in its array, as I show you in the example of creating a Custom Post Type using code.

Does your CPT have this support?

Then you can take advantage of both the block editor and the REST API and display the number of posts from another website.

Are you working with a Custom Post Type created by a third-party plugin and it does not allow you to modify it so easily?

That’s a quick fix with WordPress hooks. 😉

Snippet to support the REST API in a Custom Post Type

Add the following code at the end of functions.php or in your functionality plugin:

To see this and another 847 code snippets of this website, login or subscribe here.

In this block you use the register_post_type_args hook so that the Custom Post Type “Course” has support for the WordPress REST API.

Of course, you can change the slug to the one of the Custom Post Type you are interested in.

Conclusion

Now you know how to add support for the WordPress REST API in a Custom Post Type created by a third-party plugin.

Any questions? Let me know in the comments.

And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉

CPT PHP REST API WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?