Here is my code
/**
* Create new instance of cmb2 meta box for each bethouse fields
*/
// Get all bethouses
$bethouses = get_posts( array(
'post_type' => APP_POSTTYPE_BETHOUSE,
'posts_per_page' => -1,
) );
if ( $bethouses ):
$i = 1;
foreach( $bethouses as $bethouse ):
$for_countries = (array) get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_FOR_COUNTRIES , true );
$for_countries = implode( ', ', $for_countries );
$terms = get_the_terms( $bethouse->ID, APP_TAXONOMY_BETHOUSE );
$cats = array();
if ( $terms ) {
foreach( $terms as $term ) {
$cats[] = $term->name;
}
}
$taxonomies = implode( ', ', $cats );
$meta_box = new_cmb2_box( array(
'id' => 'bethouse_' . $bethouse->ID,
'title' => $bethouse->post_title,
'object_types' => array( APP_POSTTYPE_TOP_BETHOUSE ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'save_fields' => false
) );
$meta_box->add_field( array(
'id' => 'ct_of_' . $bethouse->ID,
'type' => 'hidden',
'save_field' => false,
'attributes' => array(
'data-countries' => $for_countries,
'data-taxonomies' => $taxonomies
)
) );
$meta_box->add_field( array(
'name' => __( 'Bethouse Active', APP_NAME ),
'id' => 'active_' . $bethouse->ID,
'type' => 'checkbox',
'attributes' => array(
'name' => 'active[' . $bethouse->ID . ']',
'checked' => ( get_post_meta( $post_id, 'active', true )[$bethouse->ID] === 'on' ? 'checked' : false )
)
) );
$meta_box->add_field( array(
'name' => __( 'Bethouse Featured Image', APP_NAME ),
'id' => 'thumbnail_' . $bethouse->ID,
'type' => 'file',
'default' => get_the_post_thumbnail_url( $bethouse->ID ),
// Optional:
'options' => array(
'url' => false,
),
'query_args' => array(
// only allow gif, jpg, or png images
'type' => array(
'image/gif',
'image/jpg',
'image/jpeg',
'image/png',
),
),
'preview_size' => array( 50, 50 ),
'attributes' => array(
'name' => 'thumbnail[' . $bethouse->ID . ']',
'value' => ( get_post_meta( $post_id, 'thumbnail', true )[$bethouse->ID] ?: get_the_post_thumbnail_url( $bethouse->ID ) )
)
) );
$meta_box->add_field( array(
'name' => __( 'Bethouse Logo', APP_NAME ),
'id' => 'logo_' . $bethouse->ID,
'type' => 'file',
'default' => get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_LOGO, true ),
// Optional:
'options' => array(
'url' => false,
),
'query_args' => array(
// only allow gif, jpg, or png images
'type' => array(
'image/gif',
'image/jpg',
'image/jpeg',
'image/png',
),
),
'preview_size' => 'full',
'attributes' => array(
'name' => 'logo[' . $bethouse->ID . ']',
'value' => ( get_post_meta( $post_id, 'logo', true )[$bethouse->ID] ?: get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_LOGO, true ) )
)
) );
$meta_box->add_field( array(
'name' => __( 'Bethouse URL', APP_NAME ),
'id' => 'url_' . $bethouse->ID,
'type' => 'text_url',
'default' => get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_URL, true ),
'attributes' => array(
'name' => 'url[' . $bethouse->ID . ']',
'value' => ( get_post_meta( $post_id, 'url', true )[$bethouse->ID] ?: get_post_meta( $bethouse->ID, APP_POSTMETA_BETHOUSE_URL, true ) )
)
) );
$meta_box->add_field( array(
'name' => __( 'Post Content', APP_NAME ),
'id' => 'post_content_' . $bethouse->ID,
'type' => 'wysiwyg',
'default' => get_the_content( null, false, $bethouse->ID ),
'options' => array(
'wpautop' => true,
'textarea_name' => "post_content[$bethouse->ID]",
'textarea_rows' => 5,
),
'attributes' => array(
'value' => ( get_post_meta( $post_id, 'post_content', true )[$bethouse->ID] ?: get_the_content( null, false, $bethouse->ID ) )
)
) );
$meta_box->add_field( array(
'name' => __( 'Side Column', APP_NAME ),
'id' => 'column_content_' . $bethouse->ID ,
'type' => 'wysiwyg',
'default' => get_post_meta( $bethouse->ID, 'column_content', true ),
'options' => array(
'wpautop' => true,
'textarea_name' => "column_content[$bethouse->ID]",
'textarea_rows' => 5,
),
'attributes' => array(
'value' => ( get_post_meta( $post_id, 'column_content', true )[$bethouse->ID] ?: get_post_meta( $bethouse->ID, 'column_content', true ) ),
)
) );
$i++;
endforeach;
endif;
// Save meta boxes fields
add_action( 'save_post', function( $post_id ) {
$slug = APP_POSTTYPE_TOP_BETHOUSE;
if ( $slug !== $_POST['post_type'] ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_REQUEST['active'] ) ) {
update_post_meta( $post_id, 'active', $_REQUEST['active'] );
}
if ( isset( $_REQUEST['url'] ) ) {
update_post_meta( $post_id, 'url', $_REQUEST['url'] );
}
if ( isset( $_REQUEST['thumbnail'] ) ) {
update_post_meta( $post_id, 'thumbnail', $_REQUEST['thumbnail'] );
}
if ( isset( $_REQUEST['logo'] ) ) {
update_post_meta( $post_id, 'logo', $_REQUEST['logo'] );
}
if ( isset( $_REQUEST['post_content'] ) ) {
update_post_meta( $post_id, 'post_content', $_REQUEST['post_content'] );
}
if ( isset( $_REQUEST['column_content'] ) ) {
update_post_meta( $post_id, 'column_content', $_REQUEST['column_content'] );
}
} );
btw my only requirement is to save data as serialized array when name
attribute set as array for a non array type field and also want to fix textarea_name
option for WYSIWYG field, in your documents it clearly stated that for WYGIWYG field
'textarea_name' => $editor_id, // set the textarea name to something different, square brackets [] can be used here
but when you will use square brackets it won’t work that’s why I wanted that changed into the function set_field_defaults_wysiwyg
to avoid overwrites during updates.
And ofcourse I am trying to achieve complex thing with your plugin and I have achieved it too but to do that I have to manually save fields using hook.