WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Widget: I'm making a custom widget with the following functionality on the admin side.

  1. 3 text fields
  2. 2 dropdown menus to select posts by title (both from a single custom post type)

Right now, instead of a drop down, the user must input the post ID# into a text field. The widget uses the post ID to grab the right data from the right posts and display it nicely on the front end. I need to be able to have the admin select a post title from the dropdown, then have the widget store the selected post's ID# in a variable and save the information.

My Code So Far This is just for the dropdown; what I've cobbled together from various examples I found around the web. I'd like to think I'm close, but I'm really stumped.

$selected = isset( $values['$bookid1'] ) ? esc_attr( $values['$bookid1'] ) : ''; 
global $post;
$args = array('post_type' => 'bookdetail','post_status' => 'publish');
$myposts = get_posts( $args );
echo '<select name="bookid1" id="bookid1">';
foreach( $myposts as $post ) :
    $bookid1 = $post->ID;   
    $displaytitle = substr( get_the_title($post->ID), 0, 50);
    echo '<option value="' . $bookid1 . '"' .  selected( $selected, $bookid1 ) .'>' . $displaytitle . '</option>';
echo '</select>';

How do I store in $bookid1 the post ID# of the post the admin user selects from the dropdown?
How do I save that variable and selection in the "save data" section of the widget code?

I appreciate any help the community could provide.

share|improve this question
Still hoping for a solution here... my own searching has been fruitless. I know this is possible, but I can't find any explanation on how to do it. I edited the question to make it clearer and less wordy. – Joe Apr 15 '12 at 23:35

Here is what I discovered and use, maybe it will help you:

function custom_posttype_dropdown($name, $selected)
    $out = "";
    $posts = get_posts(
            'post_type'  => 'bctap_templates',
            'numberposts' => -1
    if($posts )

        $out = '<select name="'.$name.'" ><option>Select a Template</option>';
        foreach( $posts as $p )
            if ($p==$selected)
                $selected = "selected = 'selected'";
                $selected = "";
            $out .= '<option value="' . $p->ID . '" '.$selected.'>' .$p->post_title  . '</option>';
        $out .= '</select>';
        $out = '<select name="'.$name.'" value="x" ><option>Select a Option</option>';
        foreach( $posts as $p )
            $out .= '<option value="x" '.$selected.'>None Created Yet.</option>';
        $out .= '</select>';
    return $out;

//to call
echo custom_posttype_dropdown($name, $selected);
share|improve this answer

Your Answer


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.