Tet banner

Custom Fields
Support MB Custom Table Custom Table with Group fields

  • Creator
  • #11571
    Resolved Lisa Tally

    What is the proper way to create a custom table and store data for custom fields that contain groups with nested fields?

    I have created a meta box with the Builder. The meta box has a number of groups that each contain one or more fields.

    When I choose not to use a custom table, WordPress stores each group's data in the wp_postmeta table. The group's name becomes the meta_key and the group's data is serialized (I believe) and stored as text in meta_value.

    In my attempt to store the data is a custom table I tried to emulate that by creating columns for each group like:

    MB_Custom_Table_API::create( "{$prefix}table_name", array(
            'group1' => 'TEXT NOT NULL',
            'group2' => 'TEXT NOT NULL',
            'group3' => 'TEXT NOT NULL'
        ) );

    But that does not seem to work, so now I wonder if there is a different way I should be doing it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
  • #11572
    Lisa Tally

    I created a more simple test and had success saving a single text field contained in a group. So now I wonder if there's something else going on.

    Test Case:
    group (id: group)
    -text (id: text)


        MB_Custom_Table_API::create( "{$prefix}testing", array(
            'group' => 'TEXT NOT NULL'
        ) );

    Setting that group to cloneable (to emulate the design of my production meta box has no affect on saving the data successfully - a good thing!)

    I'm continuing to look into this.

    Lisa Tally

    Well, I am not exactly sure what happened, but I basically started over:

    • deleted the custom table from the database
    • exported the metabox
    • unpublished the non-working metabox
    • imported the metabox (I got an error here: "Cannot load meta-box-import." but I clicked "back" and the import actually succeeded, so I continued.)
    • added test data to a page
    • ALL saved successfully...

    Weird, but I'm happy! If you want to know more details just reach out.

    Anh Tran

    Hi Purdue,

    Glad that it finally worked for you. To save groups to custom table, treat it like a normal text field. And ignore all the sub-fields in the group as they will be serialized and store in one column in the DB (the column of the group).

    If you find any trouble, please let me know.

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