WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the following code to create a widget to select a post from a dropdown - which works fine in the backend, but I can't figure out how to display this on the frontend...


class RandomPostWidget extends WP_Widget
  function RandomPostWidget()
    $widget_ops = array('classname' => 'RandomPostWidget', 'description' => 'Displays a featured post' );
    $this->WP_Widget('RandomPostWidget', 'Featured Homepage Post', $widget_ops);

  function form($instance)
    $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    $title = $instance['title'];
  <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>

  <select name="page_id" id="page_id">
 global $post;
 $args = array( 'numberposts' => -1,'category' => 5);
 $posts = get_posts($args);
 foreach( $posts as $post ) : setup_postdata($post); ?>
                <option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
 <?php endforeach; ?>


  function update($new_instance, $old_instance)
    $instance = $old_instance;
    $instance['title'] = $new_instance['title'];
    return $instance;

  function widget($args, $instance)
    extract($args, EXTR_SKIP);

    echo $before_widget;
    $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);   

    if (!empty($title))
      echo $before_title . $title . $after_title;;

    echo "<h1></h1>";

    echo $after_widget;

add_action( 'widgets_init', create_function('', 'return register_widget("RandomPostWidget");') );


I'm probably overlooking something simple - but any help would be greatly appreciated.


share|improve this question
'numberposts' => -1 can create a dropdown with 1,000,000 entries. Not a good idea. :) – toscho Apr 23 '13 at 9:53
Yeh, you're right - but it's also linked to category & for now there's only a few posts, so I'm not too concerned with that. – Dave Apr 23 '13 at 9:54
up vote 0 down vote accepted

$instance['page_id'] should be the post ID of the selected post. So …

function widget($args, $instance)
    $post = get_post( $instance['page_id'] );
    echo $post->post_content; // you should add the common filters here
    var_dump( $post ); // more data

… should be a good start.

To save the last value in the configuration form use selected():

foreach( $posts as $post )
    setup_postdata($post); ?>
        <option value="<? echo $post->ID; ?>" <?php 
        selected( $instance['page_id'], $post->ID ); 
        ?>><?php the_title(); ?></option>
share|improve this answer
Thanks - Which bit does this go in? I can't work out how to get the content from the post ID... – Dave Apr 23 '13 at 10:24
@Dave See my update. – toscho Apr 23 '13 at 10:28
aha - thanks! I've (or rather You...) have nearly cracked it. I've tidied up the code a bit - but I just have one more problem. When I select anything from the dropdown menu and click "save", it always reverts back to the top selection. Is this a problem with my $instance? (pastebin.com/dzfV3RH5) – Dave Apr 23 '13 at 10:43
@Dave See last update. – toscho Apr 23 '13 at 10:59
I can see how I missed that part in my tidying up, but now get Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION on line 58... (pastebin.com/PbgmaHUx) – Dave Apr 23 '13 at 11:13

Your Answer


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.