API – Not allowed to edit custom field
-
I have registered a custom post type named “cyc_exhibitor” and registered a custom field for that post type named “_cyc_exhibitor_first_name”. When I do a get call to the API (/wp-json/wp/v2/exhibitor/), the “_cyc_exhibitor_first_name” is shown in the meta node, but if I try to create a new exhibitor with a post call, I get the following error response:
“Sorry, you are not allowed to edit the _cyc_exhibitor_first_name custom field.”
I’m assuming the field is registered properly, since it appears in the get call response, but I can’t figure out why I can’t edit it. Here is the code for the custom post type:
function cyc_register_exhibitor_post_type () { $labels = array ( 'name' => __( 'Exhibitors' , CYC_EVENTS_DOMAIN), 'singular_name' => __( 'Exhibitor', CYC_EVENTS_DOMAIN ), 'add_new' => __( 'Add New Exhibitor', CYC_EVENTS_DOMAIN), 'add_new_item' => __( 'Add New Exhibitor', CYC_EVENTS_DOMAIN), 'not_found' => __( 'No Exhibitors Found', CYC_EVENTS_DOMAIN), ); $args = array ( 'labels' => $labels, 'public' => true, 'rewrite' => array( 'has_front' => true, 'slug' => 'exhibitor' ), 'show_in_menu' => 'edit.php?post_type=cyc_session', 'menu_position' => 1000, 'menu_icon' => 'dashicons-businessperson', 'show_in_nav_menus' => false, 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', ), 'show_in_rest' => true, 'rest_base' => 'exhibitor', 'rest_controller_class' => 'WP_REST_Posts_Controller' ); register_post_type ( 'cyc_exhibitor', $args ); } // Register meta fields for use in API // First name $meta_args = array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, ); register_post_meta( 'cyc_exhibitor', '_cyc_exhibitor_first_name', $meta_args );
And here is the JSON for the post call to /wp-json/wp/v2/exhibitor/:
{ "title": "Brad Johnson", "slug": "brad johnson", "status": "publish", "meta": { "_cyc_exhibitor_first_name": "Brad" } }
Any help would be greatly appreciated.
- The topic ‘API – Not allowed to edit custom field’ is closed to new replies.