Support » Fixing WordPress » Drop down menu in Posts metabox populated with values from Custom Post Type

  • jageo


    Can anyone direct me to tutorials or discussions so I can learn how to use the values from a a custom post type in a drop down menu on the “New Post” page

    Here’s what I envision:
    I’ll make a custom post type called “Person”, which has fields called “name” & “company”.
    When my data entry person makes a new Post, I want them to see a meta box with a drop down menu populated with the names from the Person post type

    I’m basically trying to link ‘authors’ to a post without having to make users. By using a custom post type it will be easy for my data entry person to enter new names, and with a taxonomy page I can simulate an Author page.

    I’m not afraid of coding, I’m just not sure where to start.
    Many thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • I am looking for the exact same thing. Did you ever have any luck with this? I am able to make custom metaboxes but no idea how to populate it with a post type.

    You’re asking to relate one post from one to another, correct? I realise this post is from 7 months ago, but I believe this answer will benefit everyone wanting to know the same thing.

    Effectively what you need to do is create a new meta box which you can read in the WordPress add_meta_box documentation:

    However if you keep reading down on that page, you’ll see an examples section and it has example code for creating a meta box and then saving it’s contents when the post is saved.

    In the example linked, you want to find the function called ‘myplugin_inner_custom_box’ and then you’ll do all of the fetching in here with a custom loop. Something like below:

    $args = array(
        'post_type' => 'postypeyouwnthere',
        'nopaging'  => true
    $query = new WP_Query( $args );
    echo '<select name="someposts">';
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<option value="'.$post->ID.'">';
    	echo '</option>';
    echo '</select>';

    This would print out a select dropdown with all posts of your desired post type for your metabox. You would then check for the select being sent through and save the select item from the dropdown as your custom meta field value.

    Let me know if you need further clarification, but this should help you on your way.

    I used something different for the earlier problem, but have found myself in need of this again.
    Your code has tiny error, variable $query is set in one place but referred to as $the_query in the while statement. Apart from that it works fine, and shows me a dropdown box populated with the titles from my custom post type.
    Thanks 🙂

    The codex example stops short of saving the results:
    // Do something with $mydata…(see Further Reading section below)

    So I still have reading and figuring. But eventually I’ll get to the end of it 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Drop down menu in Posts metabox populated with values from Custom Post Type’ is closed to new replies.