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 am trying to create a widget that displays posts in the sidebar taking post IDs as input from the user.

The result of my code is that the widget displays just the first entered ID and ignores the others.

Example: if my input in the widget is 1236, 1234, 1235 the widget frontend will display just the post with id 1236.

This is the code of my widget:

            <?php
            /*
            Plugin Name: Post in Sidebar
            Plugin URI: 
            Description: 
            Author: 
            Version: 1.0
            Author URI: 
            */
            class postInSidebar extends WP_Widget {

                public function __construct() { // Create the Widget

                    parent::__construct(
                        'postInSidebar', // ID
                        'Post in Sidebar', // Name
                        array( 'description' => 'Post in Sidebar lets you choose some App Reviews and show them in the sidebar' )
                        ); // Description

                }

                public function widget( $args, $instance ) { // Frontend

                    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;

                    // start postInSidebar frontend

                    $wp_query = new WP_Query( array (
                        'post_type' => 'post',
                        'post__in' => array($instance['postsToShow']),
                        'orderby' => 'post__in'
                    ));

                    ?>

                    <?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

                    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                        <h2>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                <?php the_title(); ?> - <?php the_ID(); ?>
                            </a>
                        </h2>

                    </div>

                    <?php endwhile; else: ?>
                    <p>No posts yet, go to appearance -> widgets and choose which posts to show here.</p>
                    <?php endif; ?>

                    <?php wp_reset_postdata();

                    // end postInSidebar frontend

                    echo $after_widget;

                }

                public function update( $new_instance, $old_instance ) { // Update

                    $instance = $old_instance;
                        $new_instance = wp_parse_args((array) $new_instance, array(
                            'title' => '',
                            'postsToShow' => ''
                        ));
                        $instance['title'] = strip_tags($new_instance['title']);
                        $instance['postsToShow'] = strip_tags($new_instance['postsToShow']);
                    return $instance;

                }

                public function form( $instance ) { // Backend

                    $instance = wp_parse_args( (array) $instance, array(
                        'title' => '',
                        'postsToShow' => ''
                    ));
                    $title = esc_attr($instance['title']);
                    $postsToShow = esc_attr($instance['postsToShow']);
                    ?>

                    <!-- 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 esc_attr($title); ?>" />
                      </label>
                    </p>

                    <!-- Posts To Show -->

                    <p>
                      <label for="<?php echo $this->get_field_id('postsToShow'); ?>">
                        Posts To Show:
                        <input class="widefat" id="<?php echo $this->get_field_id('postsToShow'); ?>" name="<?php echo $this->get_field_name('postsToShow'); ?>" type="text" value="<?php echo esc_attr($postsToShow); ?>" />
                      </label>
                    </p>

                <?php }

            }

            add_action( 'widgets_init', function(){
                 register_widget( 'postInSidebar' );
            });
share|improve this question

2 Answers 2

up vote 0 down vote accepted

Try to replace

'post__in' => array($instance['postsToShow']),

with

'post__in' => explode(",", $instance['postsToShow']),

to feed the post__in with an array.

Your current array looks like

array("123,456,789")

but should be like

array("123", "456", "789")
share|improve this answer
    
Thank you so much!!! You don't know how long i troubled for this! The italian healthcare has a mad person less now because i was about to visit them! –  Nicola May 15 '13 at 15:49
    
hehe, this happens to the best of us ;-) Good luck with your project. –  birgire May 15 '13 at 15:54

If $instance['postsToShow'] is a comma-separated string of IDs, then array($instance['postsToShow']) creates an array consisting of one element equal to literally 1236, 1234, 1235, which is not what you want.

Use php's explode to convert your string to an array:

'post__in' => explode( ',', $instance['postsToShow'] )
share|improve this answer
    
Thank you so much!!! You don't know how long i troubled for this! The italian healthcare has a mad person less now because i was about to visit them! –  Nicola May 15 '13 at 15:49

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.