Custom Fields
Re-Envisioned
Support MB Custom Table Same metabox fields in diferents CPT and diferents Custom Tables

This topic contains 4 replies, has 2 voices, and was last updated by  proyectohappyweb@gmail.com 6 months, 3 weeks ago.

  • Creator
    Topic
  • #14249
    Resolved
    proyectohappyweb@gmail.com
    Participant

    Hello, I have a metabox fields that are the same fields in some CPT.
    Is for that, that I write this:

    'post_types' => array( 'local' , 'negocio' ),

    But I want use MB Custom table, each table to each CPT, for this reason I write this:

    'storage_type' => 'custom_table',    // Important
    'table'        => array( 'locales' , 'negocios' ),

    But I think that ‘table’ not supported the array field.

    It’s is posible to do this?
    Or I have to duplicate the metabox fieds?

    $meta_boxes[] = array (
        'id' => 'location',
        'title' => 'Códigos Post',
        'post_types' => array( 'local' , 'negocio' ), //$post_types,
            'context' => 'after_title',
            'style'   => 'seamless',
            'priority' => 'high',
            'geo' => array(
                'types' => array( 'establishment' )
            ),
            'storage_type' => 'custom_table',    // Important
            'table'        => array( 'locales' , 'negocios' ), //'locales', // Your custom table name
            'fields' => array(
                // Address field.
                array(

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #14262

    Hello,don’t worry about this, I created the own metabox fields for each custom table.

    Thanks.

    #14310

    Hello, sorry, I would like to reopen this thread.
    To see if I can save the fields of a metabox in different tables of the database in function of the cpt that is. Is that if I do not have to triple or quadruple many metabox fields …

    Thanks.

    #14312

    Anh Tran
    Keymaster

    Hi,

    I think to remove the duplicated code, you can store all the fields in a variable $fields and set it for 2 meta boxes (each for each post type):

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
        $fields = []; // your fields
    
        $meta_boxes[] = [
            'post_types' => 'post_type_1',
            'custom_table' => 'table_1',
            'fields' => $fields,
        ];
    
        $meta_boxes[] = [
            'post_types' => 'post_type_1',
            'custom_table' => 'table_1',
            'fields' => $fields,
        ];
    
        return $meta_boxes;
    } );

    That way, you don’t have to repeat a large part of code. Also see this tutorial.

    #14334

    What a good solution, I loved it, much easier to manage and with many fewer lines of code.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.