Widget: I’m making a custom widget with the following functionality on the admin side.
- 3 text fields
- 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.
Here is what I discovered and use, maybe it will help you: