A kid A kid - 3 months ago 21
PHP Question

How to get custom posts of plugin in WordPress by REST-API?

I am now creating an android app which planned to connect to a plugin(WP Job Manager) in my WordPress side. I installed REST-API plugin and successfully getting posts by:

mywp.org/wp-json/wp/v2/posts


However, results do not include custom posts which created by the plugin.
I am new in rest-api and I am not sure what I should do.

Thankyou!

Answer

There are solutions, but the basic idea is that the custom post type must be registered with WP REST API.

You can refer to this link: http://v2.wp-api.org/extending/custom-content-types/

It should be something like this (put it in your functions.php, "cpt" = Custom Post Type, an arbitrary name):

function wpsd_add_cpt_args() {
    global $wp_post_types;

    $wp_post_types['cpt']->show_in_rest = true;
    $wp_post_types['cpt']->rest_base = 'cpt';
    $wp_post_types['cpt']->rest_controller_class = 'WP_REST_Posts_Controller';
}
add_action( 'init', 'wpsd_add_cpt_args', 30 );
Comments