1

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.

<?php
$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 ) :
    setup_postdata($post);
    $bookid1 = $post->ID;   
    $displaytitle = substr( get_the_title($post->ID), 0, 50);
    echo '<option value="' . $bookid1 . '"' .  selected( $selected, $bookid1 ) .'>' . $displaytitle . '</option>';
endforeach;
echo '</select>';
?>

So...
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.

1
  • 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
    Commented Apr 15, 2012 at 23:35

1 Answer 1

1

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

function custom_posttype_dropdown($name, $selected)
{       
    $out = "";
    $posts = get_posts(
        array(
            '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'";
            }
            else
            {
                $selected = "";
            }
            $out .= '<option value="' . $p->ID . '" '.$selected.'>' .$p->post_title  . '</option>';
        }
        $out .= '</select>';
    } 
    else
    {
        $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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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