I'm building a simple widget which will allow the user to select a post among a list of posts.
For some reason, when I select an option, I believe it actually gets selected but the option doesn't remain in the select field.
I think my error is at the following line but I don't manage to fix it. Maybe calling the_ID is not the way to do it...:
<option value="<? echo the_ID(); ?>" <?php selected(the_ID(), $instance['post_to_display']); ?>><?php the_title(); ?></option>
Here is my code:
class Simple_Widget extends WP_Widget{
function Simple_Widget() {
$widget_ops = array(
'classname' => 'w_reports',
'description' => __('Use this widget to add a post to the sidebar', 'theme'));
$this->WP_Widget('w_reports', __('Simple_Widget', 'roots'), $widget_ops);
}
function widget($args, $instance) {
extract( $args );
$post_to_display = $instance['post_to_display'];
}
function update($new_instance, $old_instance) {
$instance['post_to_display'] = $new_instance['post_to_display'];
return $instance;
}
function form($instance) {
?>
<p>
<label for="<?php echo $this->get_field_id('post_to_display'); ?>">Choose a post to display:</label>
<select id="<?php echo $this->get_field_id('post_to_display'); ?>" name="<?php echo $this->get_field_name('post_to_display'); ?>" class="widefat">
<?php // My query
$the_query = new WP_Query( 'post_type=my-post' );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<option value="<? echo the_ID(); ?>" <?php selected(the_ID(), $instance['post_to_display']); ?>><?php the_title(); ?></option>
<?php endwhile;
wp_reset_postdata();
?>
</select>
</p>
// Just a test
<?php echo $instance['post_to_display']; ?>
What am I doing wrong? Thanks in advance for your help!