Include CPT values in another CPT?

I’m trying to add CPT values in a drop-down menu within another CPT, not sure if this is do-able. Basically I have a Store CPT and a Manager CPT, in the manager cpt i have regular posts with the title, featured image, etc and what I would like to do is when I add a new store (on the Store CPT) i’d like to have a drop-down menu filled with the titles of Managers to be selected and associated, so that on the store page I can display the store manager as well.

A point in the right direction would be appriciated.

Read More

*CPT = Custom Post Type

Related posts

Leave a Reply

2 comments

  1. Three suggestions:

    1. Create Your Own Metabox

    This is your original idea: when creating/editing the Store post type, the metabox will display a dropdown with all the available Managers of this post type.

    For that, use Bainternet’s answer to this Question as a starting point, and change the get_categories for a get_posts of your CPT.
    This will be stored as post meta and retrieved normally with a get_post_meta.


    2. Use Post 2 Posts Plugin

    wordpress.org/extend/plugins/posts-to-posts/
    I never really used this one, but I know it’s done for this kind of scenario and its author is a WordPress heavy weight…

    In both plugin cases, read the documentation for the retrieval method.


    3. Use Advanced Custom Fields Plugin

    wordpress.org/extend/plugins/advanced-custom-fields/
    Another actively maintained plugin with plenty of custom fields goodies.

    The field types Relationship or Post Object can be used for this purpose.

    Bellow the screenshots of a test configuration using the post types Movies and Actors with a Relationship field (more complex than Post Object, which would produce a simple dropdown).

    Field Configuration
    click to enlarge
    field configuration

    Field Result
    click to enlarge
    field result

  2. You can use the Posts 2 Posts plug-in, or you can do it yourself with something like the following (untested):

    <?php
    add_action( 'add_meta_boxes', 'wpa71126_add_meta_box' );
    add_action( 'save_post', 'wp71126_save_post' );
    
    function wpa71126_add_meta_box() {
        add_meta_box( 
            'wpa71126_meta_box',
            __( 'Managers', 'textdomain' ),
            'wpa71126_meta_box',
            'manager' 
        );
    }
    
    function wpa71126_meta_box( $post ) {
        $args = array(
            'post_type' => 'manager',
            'name'      => 'wpa71126_manager_dropdown'
        );
    
        wp_nonce_field( plugin_basename( __FILE__ ), 'wp71126_nonce' );
    
        echo '<label>';
        _e('Select a manager', 'textdomain' );
        wp_dropdown_pages( $args );
        echo '</label> ';
    }
    
    function wp71126_save_post( $post_id ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
            return;
    
        if ( ! wp_verify_nonce( $_POST[ 'wp71126_nonce' ], plugin_basename( __FILE__ ) ) )
          return;
    
        // Check permissions
        if ( 'manger' == $_POST[ 'post_type' ] )  {
            if ( !current_user_can( 'edit_post', $post_id ) )
              return;
        }
    
        $manager = $_POST[ 'wpa71126_manager_dropdown' ];
    
        update_post_meta( $post_id, '_manager', $manager );
    }