Embedding custom posts with REST API

I am trying to find a way to embed basing custom post infomation, such as post title and few custom metas on other website.

The easiest seems to be doing it in iFrame, but there are many "no's", you can't style it as much as you want etc. So after update to 4.7 and avaiability of REST API I though it would be great to use that, and retrievve needed values with API call. But when I try to use https://example.com/wp-json/wp/v2/posts/11 where 11 is custom post type called st_activity, I'm getting post content and all the information but my custom meta is missing.

I've registered custom post type with API support, following

http://v2.wp-api.org/extending/custom-content-types/

adding

'show_in_rest'       = true,
'rest_base'          = 'activity-api',
'rest_controller_class' = 'WP_REST_Posts_Controller',

and adjusted meta:

function slug_register_rate_review() {
            register_rest_field( 'st_activity',
                'rate_review',
            array(
                'get_callback' = 'slug_get_st_activity',
                'update_callback' = null,
                'schema' = null,
            )
        );
    }

    function slug_get_st_activity($post, $field_name, $request) {
        return get_post_meta($post['id'], 'rate_review' );
    }

Well, obviously I'm doing something wrong but have no idea what exactly.

And ddespite that, is it possible and how to get only title, link, and custom meta without all the other values? Thank you!

Topic rest-api embed Wordpress

Category Web


Your endpoint is wrong, /wp/v2/posts is just for objects with post_type='post'. If your custom post type is registered with 'activity-api' the endpoint would be /wp/v2/activity-api/11.

That's because it's the slug you registered in 'rest_base' => 'activity-api' argument to your post type. Without a custom rest_base argument, the endpoint would be at /wp/v2/st_activity

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.