Support Forum
Support › MB Custom Table › Media custom fields data not persisted in custom tables
Hi!
We have created several custom fields groups with custom database tables. All working fine except the field group used in media/attachment. The data is not saved.
I have followed this for troubleshooting, but it did not help:
https://metabox.io/wordpress-custom-fields-not-saving-increase-max-input-vars/
Again, other fields data saved with no issues in custom tables.
Thanks!
This is generated PHP as an example. NOTE: I am not registering these fields in php, I am using Meta Box web interface for all fields.
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'VoB Media Source', 'your-text-domain' ),
'id' => 'vob-media-source',
'post_types' => ['attachment'],
'media_modal' => true,
'storage_type' => 'custom_table',
'table' => 'wp_custom_vob_media_source',
'fields' => [
[
'name' => __( 'VoB Source Name', 'your-text-domain' ),
'id' => $prefix . 'vob_source_name',
'type' => 'taxonomy',
'label_description' => __( 'Name of the publisher or web-site.', 'your-text-domain' ),
'taxonomy' => ['vob-source'],
'field_type' => 'select',
'add_new' => true,
'required' => true,
],
[
'name' => __( 'VoB Source URL', 'your-text-domain' ),
'id' => $prefix . 'vob_source_url',
'type' => 'url',
'label_description' => __( 'Link to content source.', 'your-text-domain' ),
'placeholder' => __( 'Add source URL', 'your-text-domain' ),
'required' => true,
],
],
'validation' => [
'rules' => [
$prefix . 'vob_source_name' => [
'required' => true,
],
$prefix . 'vob_source_url' => [
'required' => true,
'url' => true,
],
],
],
];
return $meta_boxes;
}
This is generated PHP as an example. NOTE: I am not registering taxonomy in php, I am using Meta Box web interface for all fields.
<?php
add_action( 'init', 'your_prefix_register_taxonomy' );
function your_prefix_register_taxonomy() {
$labels = [
'name' => esc_html__( 'Sources', 'your-textdomain' ),
'singular_name' => esc_html__( 'Source', 'your-textdomain' ),
'menu_name' => esc_html__( 'Sources', 'your-textdomain' ),
'search_items' => esc_html__( 'Search Sources', 'your-textdomain' ),
'popular_items' => esc_html__( 'Popular Sources', 'your-textdomain' ),
'all_items' => esc_html__( 'All Sources', 'your-textdomain' ),
'parent_item' => esc_html__( 'Parent Source', 'your-textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Source', 'your-textdomain' ),
'edit_item' => esc_html__( 'Edit Source', 'your-textdomain' ),
'view_item' => esc_html__( 'View Source', 'your-textdomain' ),
'update_item' => esc_html__( 'Update Source', 'your-textdomain' ),
'add_new_item' => esc_html__( 'Add new source', 'your-textdomain' ),
'new_item_name' => esc_html__( 'New source name', 'your-textdomain' ),
'separate_items_with_commas' => esc_html__( 'Separate sources with commas', 'your-textdomain' ),
'add_or_remove_items' => esc_html__( 'Add or remove sources', 'your-textdomain' ),
'choose_from_most_used' => esc_html__( 'Choose most used sources', 'your-textdomain' ),
'not_found' => esc_html__( 'No sources found', 'your-textdomain' ),
'no_terms' => esc_html__( 'No Sources', 'your-textdomain' ),
'filter_by_item' => esc_html__( 'Filter by source', 'your-textdomain' ),
'items_list_navigation' => esc_html__( 'Sources list pagination', 'your-textdomain' ),
'items_list' => esc_html__( 'Sources list', 'your-textdomain' ),
'most_used' => esc_html__( 'Most Used', 'your-textdomain' ),
'back_to_items' => esc_html__( 'Back to sources', 'your-textdomain' ),
];
$args = [
'label' => esc_html__( 'Sources', 'your-textdomain' ),
'labels' => $labels,
'description' => '',
'public' => true,
'publicly_queryable' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'meta_box_cb' => true,
'show_in_rest' => true,
'show_tagcloud' => true,
'show_in_quick_edit' => true,
'show_admin_column' => false,
'query_var' => true,
'sort' => false,
'rest_base' => '',
'rewrite' => [
'slug' => 'source',
'with_front' => false,
'hierarchical' => false,
],
];
register_taxonomy( 'vob-source', [], $args );
}
Hi,
Can you please share the code that creates the custom table? Please note that the column key must match the field ID. Get more details on the documentation https://docs.metabox.io/extensions/mb-custom-table/
Another note, the field taxonomy set the post terms so it will not save data in the custom table. Please use the field taxonomy_advanced instead of taxonomy. Refer to this topic https://support.metabox.io/topic/custom-fields-are-not-saving-to-custom-table/
Hi Long,
I have used Meta Box web UI to create tables and it all looks correct in DB. Also, similar fields but added to a custom post type work as expected.
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'VoB Media Source', 'your-text-domain' ),
'id' => 'vob-source-media',
'post_types' => ['attachment'],
'media_modal' => true,
'storage_type' => 'custom_table',
'table' => 'wp_custom_vob_source_media',
'fields' => [
[
'name' => __( 'VoB Source Name', 'your-text-domain' ),
'id' => $prefix . 'vob_source_name',
'type' => 'taxonomy_advanced',
'label_description' => __( 'Name of the publisher or web-site.', 'your-text-domain' ),
'taxonomy' => ['vob-source'],
'field_type' => 'select',
'add_new' => true,
'required' => true,
],
[
'name' => __( 'VoB Source URL', 'your-text-domain' ),
'id' => $prefix . 'vob_source_url',
'type' => 'url',
'label_description' => __( 'Link to content source.', 'your-text-domain' ),
'placeholder' => __( 'Add source URL', 'your-text-domain' ),
'required' => true,
],
],
'validation' => [
'rules' => [
$prefix . 'vob_source_name' => [
'required' => true,
],
$prefix . 'vob_source_url' => [
'required' => true,
'url' => true,
],
],
],
];
return $meta_boxes;
}
Your SQL query has been executed successfully.
DESCRIBE <code>wp_custom_vob_source_media</code>
ID bigint(20) unsigned NO PRI
NULL
vob_source_name text YES
NULL
vob_source_url text YES
NULL
Hi,
Thanks for the additional information.
I got it. The media_modal
setting does not work with the custom table. Please remove it to save the media field value in the custom table. I will inform the development team to fix this issue in future updates.
Thanks, Long! I have also come to that conclusion.
We would appreciate for a prompt fix. We have been evaluating ToolSet and Meta Box for our use cases and ability to add custom fields to Media Modal was one of the selling point as it saves our editors from additional clicks and is more straightforward.