0

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' );
            });

2 Answers 2

0

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")
2
  • 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
    Commented May 15, 2013 at 15:49
  • hehe, this happens to the best of us ;-) Good luck with your project.
    – birgire
    Commented May 15, 2013 at 15:54
1

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'] )
1
  • 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
    Commented May 15, 2013 at 15:49

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.