WordPress.org

Ready to get started?Download WordPress

Forums

Drop down menu in Posts metabox populated with values from Custom Post Type (4 posts)

  1. jageo
    Member
    Posted 3 years ago #

    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!
    JA

  2. adam533
    Member
    Posted 2 years ago #

    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.

  3. Beggars
    Member
    Posted 2 years ago #

    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: http://codex.wordpress.org/Function_Reference/add_meta_box

    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. http://codex.wordpress.org/Function_Reference/add_meta_box#Example

    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.'">';
    	the_title();
    	echo '</option>';
    endwhile;
    
    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.

  4. jageo
    Member
    Posted 2 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic