2

I am using the following plugins to get JSON data from WordPress REST API:

Advanced Custom Fields PRO WP REST API ACF to REST API

I have a created custom post type called "Custom Navigation". I have a custom field group (through ACF) called "Custom Navigation Item" that is only applied when the post type is Custom Navigation. The fields in this group are called "Image" and "Caption", which I can't get the values for.

Here is where I set my menus and add filters for the ACF to REST API plugin. These work as intended.

function custom_setup() {
    // This theme uses wp_nav_menu() in two locations.
    register_nav_menus( array(
        'topnav'    => 'Top Menu',
        'footer-left-nav' => 'Footer Top Menu 1',
        'footer-center-left-nav' => 'Footer Top Menu 2',
        'footer-center-right-nav' => 'Footer Top Menu 3',
        'footer-right-nav' => 'Footer Top Menu 4',
        'footer-bottom-left-nav' => 'Footer Bottom Menu 1',
        'footer-bottom-right-nav' => 'Footer Bottom Menu 2',
    ) );

    // Enable the option show in rest
    add_filter( 'acf/rest_api/field_settings/show_in_rest', '__return_true' );

    // Enable the option edit in rest
    add_filter( 'acf/rest_api/field_settings/edit_in_rest', '__return_true' );
}

add_action( 'after_setup_theme', ‘custom_setup' );

This function sets my custom post type menu items to the WP REST API. This works as intended in so far as doing this. The ACF fields of these post type menu items however do not send

function wp_api_v2_custom_get_menu_data ($data) {

    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $data['id'] ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[$data['id']] );
    $menuItems = wp_get_nav_menu_items($menu->term_id);

    return $menuItems;
    } else {
        return array();
    }
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z(-]+)', array(
        'methods' => 'GET',
        'callback' => 'wp_api_v2_custom_get_menu_data',
    ) );
} );

Here is my function that sets my custom post type

function custom_nav_post_type() {
    register_post_type(‘custom_nav',
        array (
            'name'             => ‘Custom Navigation',
            'singular_name'        => 'CustomNav',
            'can_export'        => TRUE,
            'exclude_from_search'    => FALSE,
            'has_archive'        => TRUE,
            'hierarchical'        => TRUE,
            'label'            => 'Custom Navigation',
            'menu_position'        => 5,
            'public'            => TRUE,
            'publicly_queryable'    => TRUE,
            'query_var'        => ‘customnav',
            'rewrite'            => array ( 'slug' => 'shop' ),
            'show_ui'        => TRUE,
            'show_in_menu'    => TRUE,
            'show_in_nav_menus'    => TRUE,
            'show_in_rest'        => TRUE,
            'rest_base'        => 'shop',
                'rest_controller_class'    => 'WP_REST_Posts_Controller',
            'supports'        => array ('title')
        )
    );
}
add_action( 'init', 'custom_nav_post_type' );
?>

The output from this all of this

[
{
    "id":184,
    "acf":[

    ]
},
{
    "id":183,
    "acf":[

    ]
},
{
    "id":182,
    "acf":[

    ]
},
{
    "id":181,
    "acf":[

    ]
},
{
    "id":180,
    "acf":[

    ]
},
{
    "id":176,
    "acf":[

    ]
}
]

Any advice on where I am going wrong would be greatly appreciated.

2
  • Please, consider minimizing your code to fit the MCVE statement. Commented Mar 28, 2017 at 5:48
  • Edited to make hopefully a bit clearer, apologies Commented Mar 28, 2017 at 7:49

1 Answer 1

2

Here is how I solved it. I was missing a filter for my custom post type which I added to my "custom_setup" function.

add_filter('rest_prepare_custom_nav', function($response) {
     $response->data['acf'] = get_fields($response->data['id']);
     return $response;
});

So the filter is a wild card filter, applied like

apply_filter("rest_prepare_YOUR-CUSTOM-POST-TYPE-NAME-HERE", ...
Sign up to request clarification or add additional context in comments.

1 Comment

Hello @Allan Fitzpatrick, why the "..." please complete the code snippet. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.