0

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!

2 Answers 2

1

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>
7
  • 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
    Commented Sep 3, 2012 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
    Commented Sep 4, 2012 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
    Commented Sep 6, 2012 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
    Commented Sep 6, 2012 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
    Commented Sep 7, 2012 at 7:38
0

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>

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.