You have to use get_results()
instead of WP_Query
.
I have made changes in your code and tested at my end and it is working for me. I have also save that value using update()
function of widget. You can take reference from below code.
public function form( $instance ) {
if ( isset( $instance[ 'mydropdown' ] ) )
$selectedvalue = $instance[ 'mydropdown' ];
else
$selectedvalue = __( '', 'text-domain' );
global $event;
global $events;
global $wpdb;
$events = $wpdb->get_results("SELECT id, event FROM mo_Event",ARRAY_A);
?>
<select class="dropdown" id="<?php echo $this->get_field_name( 'mydropdown' ); ?>" name="<?php echo $this->get_field_name( 'mydropdown' ); ?>" title="My Dropdown">
<?php
foreach ($events as $event) {
$selected = ($event->id == $selectedvalue) ? 'selected' : '';
echo '<option value="'.$event->id.'" '.$selected.'>'.$event->event.'</option>';
}
?>
</select>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['mydropdown'] = ( ! empty( $new_instance['mydropdown'] ) ) ? strip_tags( $new_instance['mydropdown'] ) : '';
return $instance;
}