black friday sale

Custom Fields
Re-Envisioned
Support MB Custom Table Custom Table Primary and Foreign Key

  • Creator
    Topic
  • #21146
    Resolved EricHedvat
    Participant

    I have created a custom table for meta fields using the guide.
    The ID field has been automatically added. It is a primary key, yet it displays the post id which the meta field belongs to.
    When I delete that post from wordpress, the custom table entries remain.
    How can i reconfigure it so ‘ID’ is an autoincrementing primary key, and ‘post_id’ is a foreign key, referencing the post id, and enabling the entry to be deleted when the post is deleted?

    Here is the code used for custom table

    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',
        ) );
    }

    Thanks

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

    Hi,

    When you delete the post from Trash, it removes the row in the custom table as well. See my screen record https://www.loom.com/share/218bb42c044045468f7355e20a3aa6f6.

    You can also set KEY for another column by using the third parameter.

    MB_Custom_Table_API::create( 'my_custom_table', array(
        'address' => 'TEXT NOT NULL',
        'phone'   => 'TEXT NOT NULL',
        'email'   => 'VARCHAR(20) NOT NULL',
    ), array( 'email' ) );
    
    #21153
    EricHedvat
    Participant

    Working thanks!

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