Custom Fields
Re-Envisioned
Support MB Custom Table Custom Table with Group fields

This topic contains 3 replies, has 2 voices, and was last updated by  Anh Tran 10 months, 2 weeks ago.

  • Creator
    Topic
  • #11571
    Resolved
    Purdue Alumni Association
    Participant

    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
    Replies
  • #11572

    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)

    Code:

        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.

    #11573

    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.

    #11575

    Anh Tran
    Keymaster

    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.