Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

For some reason, this (found here) works. However, I still haven't managed to adapt it to do what I want to do. Therefore, this answers only partially my question.

<select id="<?php echo $this->get_field_id('posttype'); ?>" name="<?php echo $this->get_field_name('posttype'); ?>" class="widefat" style="width:100%;">
  <?php foreach(get_post_types($getposttype_args,'names') as $post_type) { ?>
    <option <?php selected( $instance['posttype'], $post_type ); ?> value="<?php echo $post_type; ?>"><?php echo $post_type; ?></option>
  <?php } ?>      
</select>
share|improve this answer

Use get_the_ID() instead of the_ID(). the_ID() will echo'ed the ID of posting:

<option value="<? the_ID(); ?>" <?php selected(get_the_ID(), $instance['post_to_display']); ?>><?php the_title(); ?></option>
share|improve this answer
    
Thank you, but it doesn't work either. The ID is passed and saved but the name of the option selected is not "recorded". It goes back to the first choice by default. –  Macxim Sep 3 '12 at 8:27
    
Ok, go to control if the ID are coming: <?php while ( $the_query->have_posts() ) : $the_query->the_post(); echo get_the_ID(); ?> If the ID are printed, capsulate it into a variable and use this. –  WPler Sep 4 '12 at 7:25
    
The IDs don't print. Something is definitely wrong. However, my test <?php echo $instance['post_to_display']; ?> does returns an ID... –  Macxim Sep 6 '12 at 9:15
    
Ok, use it in another way directly by the Query: `<?php foreach($the_query->posts as $post) : $postid = $post->ID; ?>...<?php endforeach; ?> –  WPler Sep 6 '12 at 13:12
    
I don't understand. This doesn't return anything either. But my previous test keep returning the IDs when I select posts. –  Macxim Sep 7 '12 at 7:38

Your Answer

 
discard

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.