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

Pages and custom post types issue with WP-API v2

chrishutchinson opened this issue · comments

I need to do more digging into this, but v2.0-beta4 doesn't seem to handle custom post types very well, so no ACF data comes through there yet.

This doesn't affect users with version 1.x of WP-APi.

Is this related to custom post types having to opt-in to the rest api?

show_in_rest=true

WP-API/WP-API#1195

@wkw It doesn't seem so. Enabling the show_in_rest=true enables the custom post type in the REST API but these posts do not include any ACF data. I'll have a look in the plugin code, but any pointers would be greatly appreciated.

edit: I've had a quick glance and it seems that the register function is only working for the post type post and none other. If I add a register function and corresponding add action for my custom post type to the plugin, it works like a charm

function addACFDataBookV2() {
    register_api_field( 'book',
            'acf',
            array(
                'get_callback'    => array( $this, 'addACFDataPostV2cb' ),
                'update_callback' => null,
                'schema'          => null,
            )
    );
}

add_action( 'rest_api_init', array( $this, 'addACFDataBookV2' ) ); // CPT Books

edit2: okay I've temporarily fixed this with the help of the following blogpost. Apparantly you can get all custom post types in an array like this:
$types = get_post_types(array('public' => true,'_builtin' => false));
And so I've added a register_api_field function using the $types array. Full code

    function addACFDataCustomV2() {
        $types = get_post_types(array('public'   => true,'_builtin' => false));
        register_api_field( $types,
            'acf',
            array(
                'get_callback'    => array( $this, 'addACFDataPostV2cb' ),
                'update_callback' => null,
                'schema'          => null,
            )
        );
    }

add_action( 'rest_api_init', array( $this, 'addACFDataCustomV2' ) );

I'll fork the plugin and add this. Don't know if it's the correct solution but If wanted I'll do a pr!

Hey @aderaaij,

Thanks for clarifying that. I've actually been planning to add code very similar to the above for the past week or so, and have an almost-completed version running of my machine at the moment.

I've not yet had chance to finish it off and push it up. Happy for you to do a PR as long as everything is commented, indented and structured as per the rest of the repo.

If you don't get chance, I'll aim to get this done by the beginning of next week!

Any joy on this Chris? ie. adding code to core?

@chrishutchinson that's perfect! I haven't done the PR request yet as I'm just a front-ender faking PHP skills, but I was wondering the follow though: Is there a particular reason why you've written separate functions for getting the page, posts, etc? I'm thinking $types = get_post_types(array('public' => true,'_builtin' => true)); would work just as well?

Thanks @aderaaij ! Saved me some trouble 👍

Thanks for all feedback / suggestions everyone - aiming to have this sorted in the plugin this week, will keep you all posted! 👍

Thanks @aderaaij, this was frustrating me all day, thought I was doing something wrong. For now I'm using your version but will change over when the plugin has been updated officially.

Your welcome guys. thx Chris for the great plugin. I couldn't PHP my way out of a paper bag so without this plugin I would've been nowhere.

Oh just for the record, there's a PR for this same issue in this repo. It does the same as my changes, albeit a bit different: #22

Hey all,

This should now be fixed. I've added a new release in GitHub and pushed the changes to the WordPress Plugin repository!

Please re-open this issue if I missed anything, and thanks again for all your help!

@chrishutchinson https://github.com/chrishutchinson worked like a charm
for me chris - thanks! r

:::::::::::::::::::::::

Richard Max
07882 259888

Sent from mobile

On 17 September 2015 at 16:12, Liam Neville notifications@github.com
wrote:

Hey @chrishutchinson https://github.com/chrishutchinson, thanks for
updating, although I just updated the plugin and can't see any acf data
anymore.


Reply to this email directly or view it on GitHub
#21 (comment).

yup, works like a charm dude, thanks. I'll kill my fork! (strange sentence indeed).