Same metabox fields in diferents CPT and diferents Custom Tables

Support MB Custom Table Same metabox fields in diferents CPT and diferents Custom TablesResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #14249
    proyectohappyweb@gmail.com[email protected]
    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.

    #14262
    proyectohappyweb@gmail.com[email protected]
    Participant

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

    Thanks.

    #14310
    proyectohappyweb@gmail.com[email protected]
    Participant

    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 TranAnh 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
    proyectohappyweb@gmail.com[email protected]
    Participant

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

    Thank you.

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