You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
WP REST API shows all metaboxes for all custom post types #1158
When registering metaboxes for two different custom post types to be visible in the REST API (using /wp/v2/{post_type}) the 'cmb2' object in REST response will only contain the metaboxes for that post type.
Actual Behavior:
The REST response for both post types contains all metaboxes, albeit the fields in the boxes not registered to that post type all have empty values
Steps to reproduce (I have confirmed I can reproduce this issue on the trunk branch):
Register two custom post types, each with show_in_rest => true
Create a CMB2 metabox for each post type, again with show_in_rest => true
Ensure they are initialised on the cmb2_init hook instead of cmb2_admin_init
Inspect the REST API response for either post type - they should show object for both metaboxes under the cmb2 key, the correct one will be populated with values, the other will not
CMB2 Field Registration Code:
// Metabox for 'office' CPTadd_action( 'cmb2_init', 'office_register_metabox' );
functionoffice_register_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'office_description_metabox',
'title' => esc_html__( 'Office info', 'domain' ),
'object_types' => 'office',
'show_in_rest' => true,
) );
// Fields are irrelevant - they could be any fields$cmb->add_field( array(
// Field Config...
) );
}
// Metabox for 'country' CPTadd_action( 'cmb2_init', 'country_register_metabox' );
functioncountry_register_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'country_description_metabox',
'title' => esc_html__( 'Country info', 'domain' ),
'object_types' => 'country',
'show_in_rest' => true,
) );
// Fields are irrelevant - they could be any fields$cmb->add_field( array(
// Field Config...
) );
}
Activity
Mte90 commentedon Mar 12, 2019
I can confirm this issue, I am working for a project to redo all the endpoint to avoid this problem.
Mte90 commentedon Mar 12, 2019
Seems that https://github.com/CMB2/CMB2/blob/develop/includes/rest-api/CMB2_REST.php#L404 rest_box include the right metabox but read_fields return all the fiedls also of other metabox
Mte90 commentedon Mar 12, 2019
And I did a pr with the bugfix #1238 :-)
Merge remote-tracking branch 'upstream/develop' into develop