• Resolved momofone

    (@jacquidervan)


    Hello,

    I have a meta box with 8 custom fields shared between two post types. Can I choose to have only one of those post types exposed to the REST API (Readable only)?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I would assume yes, but that would be something taken care of at the post type registration level, and not at a metabox setup level.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    I think you’ll have to register two separate boxes in order to do that. Something like this if you don’t want to duplicate all the field code:

    <?php 
    add_action( 'cmb2_init', 'yourprefix_register_cpt_metaboxs' );
    /**
     * Hook in and add a metabox that only appears on the 'About' page
     */
    function yourprefix_register_cpt_metaboxs() {
    
    	$cmb_books = new_cmb2_box( array(
    		'id'           => '_yourprefix_book_cpt_metabox',
    		'title'        => __( 'Books Metabox', 'cmb2' ),
    		'object_types' => array( 'book' ),
    		'show_in_rest' => WP_REST_Server::READABLE, // show in REST API
    	) );
    
    	yourprefix_register_shared_cpt_fields( $cmb_books );
    
    	$cmb_magazines = new_cmb2_box( array(
    		'id'           => '_yourprefix_magazine_cpt_metabox',
    		'title'        => __( 'Magazines Metabox', 'cmb2' ),
    		'object_types' => array( 'magazine' ),
    		'show_in_rest' => false, // No REST API for this cpt
    	) );
    
    	yourprefix_register_shared_cpt_fields( $cmb_magazines );
    }
    
    function yourprefix_register_shared_cpt_fields( $cmb ) {
    
    	$cmb->add_field( array(
    		'name' => __( 'Publisher', 'cmb2' ),
    		'desc' => __( 'field description (optional)', 'cmb2' ),
    		'id'   => '_yourprefix_publisher',
    		'type' => 'text',
    	) );
    
    	// Add other shared fields here.
    }
    • This reply was modified 6 years, 9 months ago by Justin Sternberg. Reason: fix typo
    • This reply was modified 6 years, 9 months ago by Justin Sternberg. Reason: fix another typo
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Also what Justin said just now ?? He will know more on the topic than myself at the moment

    Thread Starter momofone

    (@jacquidervan)

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘two post types sharing box and tax’ is closed to new replies.