times / acf-to-wp-api

Puts all ACF fields from posts, pages, custom post types, attachments and taxonomy terms, into the WP-API output under the 'acf' key

Home Page:http://wordpress.org/plugins/acf-to-wp-api/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom Post Types

harrythree opened this issue · comments

I'm using V2 of the WP-API and I'm trying to get my custom post types. I'm using Version: 1.3.2 of acf-to-wp-api.

I tried making a request with Postman to this endpoint...

http://localhost:8888/wp-json/wp/v2/car

With 'car' being my custom post type of course. But I get this response...

[
    {
        "code": "rest_no_route",
        "message": "No route was found matching the URL and request method",
        "data": {
            "status": 404
        }
    }
]

I found this blog post that goes through setting up getting custom post types with WP-API V2 (https://bay-a.co.uk/wordpress-tips/wp-api-v2-tips/)

When I added this to my functions.php in my theme, I could get my custom post type response.

add_action( 'init', 'add_events_to_json_api', 30 );
function add_events_to_json_api(){

    global $wp_post_types;
    $wp_post_types['car']->show_in_rest = true;
    $wp_post_types['car']->rest_base = 'car';
    $wp_post_types['car']->rest_controller_class = 'WP_REST_Posts_Controller';
}

I looked through the plugin code and I can see where custom post types are registered, but it doesn't seem to be working for me. Maybe my url is incorrect?

Any help would be awesome! Thanks!

Hey @harrythree,

Can you also share the code where the custom post type is registered? I'll do some testing and see if I can recreate the issue you're having!

@chrishutchinson The custom post type was created using the Custom Post Types UI plugin

https://wordpress.org/plugins/custom-post-type-ui/

So I'm not actually registering myself, the plugin registers them for me. Maybe that's the problem?

Try going to Settings > Permalinks page once the WP-API 2 plugin is installed. I had to do this for the new routes to register. Not sure why we had to do this, but we banged our heads for about 2 hours trying to figure out why the new endpoints weren't present. This happened on both our staging and production sites.