Custom Fields
Re-Envisioned
Support MB Custom Table Custom table - data storing error - manual code.

  • Creator
    Topic
  • #28256
    Resolved Prabakaran Shankar
    Participant

    Hi,

    I'm new to this plugin. I have tried the documentation to create a custom table. As mentioned, it has created in the database. But, custom fields are not updated in the table, instead, it saved in the ' _post' table.

    I have followed the below code as mentioned. I don't know to find the error. Also, I have created the table via PHPMyAdmin and used the code from 'add filter.....', this option also not works.

    for your kind information, I have tried the code displayed in the youtube tutorial.

    add_action( 'init', 'prefix_create_table' );
    function prefix_create_table() {
        if ( ! class_exists( 'MB_Custom_Table_API' ) ) {
            return;
        }
        MB_Custom_Table_API::create( 'my_custom_table', array(
            'address' => 'TEXT NOT NULL',
            'phone'   => 'TEXT NOT NULL',
            'email'   => 'VARCHAR(20) NOT NULL',
        ) );
    }
    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            'title'        => 'Meta Box Title',
            'storage_type' => 'custom_table',    // Important
            'table'        => 'my_custom_table', // Your custom table name
            'fields'       => array(
                array(
                    'id'   => 'address',
                    'type' => 'text',
                    'name' => 'Address',
                ),
                array(
                    'id'   => 'phone',
                    'type' => 'text',
                    'name' => 'Phone',
                ),
                array(
                    'id'   => 'email',
                    'type' => 'email',
                    'name' => 'Email',
                ),
            ),
        );
        return $meta_boxes;
    }

    In the auto function, custom table creation works fine.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #28262
    Long Nguyen
    Moderator

    Hi Prabakaran,

    For the default post, the code saves the field value in the custom table as well. For custom post types, please add the post_types settings when registering the meta box to save the field value of post types to the custom table.

    $meta_boxes[] = array(
        'title'        => 'Meta Box Title',
        'storage_type'  => 'custom_table',    // Important
        'table'         => 'my_custom_table', // Your custom table name
        'post_types'    => 'post-type-slug' // Here
        ...
    )
    #28280
    Prabakaran Shankar
    Participant

    Thank you very much for the suggestion, however, I have found another way, which stores the value in the custom table.

    But, I have noticed another event in the DB tables.
    Briefly, I have created a custom table with the custom fields via PHPmyadmin. Later on, pointed the custom table to the custom fields through the option "save data in the custom table" without selecting the create a custom table.
    Then, created a new post in the custom post type (for example: Event). Accordingly, custom fields are saved in the custom date table for the post id 120 as follows:
    https://1drv.ms/u/s!AjW7znckfYbzg9FQr9W0KVUtCBqavw?e=uY6PSO

    Similarly, I have noticed that the value for post id 120 saved in the _post and _post_meta table also, as follows.
    https://1drv.ms/u/s!AjW7znckfYbzg9FR-sAypFoA5SCBew?e=GFEcdS
    https://1drv.ms/u/s!AjW7znckfYbzg9FSLW_iyO0TVU-JIA?e=Y8wrMZ

    is that normal, I have missed any configurations or I have to add any other additional codes to avoid additional savings in the post and post meta tables.

    #28289
    Long Nguyen
    Moderator

    Hi,

    Follow your screenshots, I see the custom fields event_name and event_date save their value in the custom table and do not save in the postmeta table. Everything is working fine.

    In the table posts, the post title also update with the event_name value, it is possible that you've added a custom code to update the post title by using custom field value. Please re-check it.

    In the table postmeta, there are some meta keys default of WordPress such as _edit_lock, _edit_last, they are not encouraged to remove. You can deactivate the plugin RankMath and remove the custom field single_image to avoid saving their value in the database.

    #28314
    Prabakaran Shankar
    Participant

    Thank you so much.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.