Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing custom field in REST response for all items #115

Closed
Huppys opened this issue Mar 16, 2017 · 1 comment
Closed

Missing custom field in REST response for all items #115

Huppys opened this issue Mar 16, 2017 · 1 comment

Comments

@Huppys
Copy link

Huppys commented Mar 16, 2017

Hey,

I were able to implement a modified REST response by using:

add_filter( 'acf/rest_api/fishes/get_fields', function( $data )
{
    if ( method_exists( $data, 'get_data' ) )
    {
        $data = $data->get_data();
    } else {
        $data = (array) $data;
    }

    if ( isset( $data['acf'] ) && ! $data['acf'] ) {
        $data['acf'] = array();
    }

    add_custom_fields_to_response ($data); // I pass $data by reference

    return $data;
} );

When I call wp-json/acf/v3/fishes/291 I got my custom field added to the default fields. That's fine.

But I'm missing these custom fields when calling all fishes by wp-json/acf/v3/fishes.

I created a custom REST route:

add_action( 'rest_api_init', 'register_custom_routes', 10 );

function register_custom_routes() {
    register_rest_route( 'acf/v3', '/fishes', array(
        array(
            'methods'  => WP_REST_Server::READABLE,
            'callback' => 'get_fishes',
            'args' => array (
              'custom_post_type' => 'fishes'
            )
        )
    ) );
}


function get_fishes(WP_REST_Request $request) {
    $filter = $request->get_param( 'filter' );
    $data   = array();

    $args = array(
        'posts_per_page' => -1,
        'post_type'      => 'fishes',
    );

    if ( is_array( $filter ) && array_key_exists( 'category', $filter ) ) {
        $args['category_name'] = $filter['category'];
    }

    $posts = get_posts( $args );

    if ( ! empty( $posts ) ) {
        foreach( $posts as $post ) {
            $acf = get_fields( $post->ID );
            if ( ! empty( $acf ) ) {
                $data[] = array_merge( array( 'id' => $post->ID ), $acf );
            }
        }
    }

    return $data;
}

Did I miss something? Please could someone shed some light on this?

Best,
huppys

@airesvsg
Copy link
Owner

Hi @Huppys,
Please, read this issue #109
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants