Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

*CPT = Custom Post Type

share|improve this question

2 Answers 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 );
}
share|improve this answer
    
How would I show that in the front end though? –  Beto Nov 15 '12 at 19:46
    
You can still use wp_dropdown_pages() on the front end, but I am not sure how because I do not have your code in front of me. Please update your question with the code that you are using to do the front end editing. –  Joseph Nov 15 '12 at 20:28

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

share|improve this answer
    
Wow thanks for your reply, this very helpful. –  Beto Nov 18 '12 at 7:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.