2

During making a popular post widget I see this notice:

Notice: Undefined index: popular_posts_order_by in /hermes/bosnaweb09a/b2039/ipg.tutorialcoursecom/arifhossin/wp-content/themes/testtheme/framework/widgets/pb-popular-post.php on line 26.

Following is my Code..Can anybody solve this?

<?php


add_action( 'widgets_init', 'personalblog_popular_posts_widget' );
    function personalblog_popular_posts_widget() {
        register_widget( 'Personalblog_Popular_Posts' );
    }

    class Personalblog_Popular_Posts extends WP_Widget{
        // Initialize the widget
        public function __construct() {
            parent::__construct(
                'personalblog_popular_posts_widget', esc_html__('Strawberry: Popular Posts Widget','strawberry'), array('description' => esc_html__('Strawberry:A widget that shows Polupar Posts', 'strawberry')));  
        }

        // Output of the widget
        public function widget( $args, $instance ) {
            extract($args);
            $title = apply_filters('widget_title', $instance['title']);
            $popular_posts_order_by = $instance['popular_posts_order_by'];
            $poular_posts_time_range = $instance['poular_posts_time_range'];
            $num_fetch = $instance['num_fetch'];

            // Opening of widget
            echo $before_widget;

            // Open of title tag
            if( !empty($title) ){ 
                echo $before_title . $title . $after_title; 
            }
            $args = array(
            'post_type'             => 'post',
            'posts_per_page'        => $num_fetch,
            'ignore_sticky_posts'   => true,
            'no_found_rows'         => true,
            'post_status'           => 'publish',
            'orderby'               => $popular_posts_order_by,
            'order'                 => 'DESC',
            'date_query' => array(
                array(
                    'after'  => $poular_posts_time_range,
                ),
            ),
        );

        $posts_query = new WP_Query($args); ?>

        <?php if ( $posts_query->have_posts() ) { ?>
            <div class="pb-recent-posts-widget-section clearfix">
                <ul class="pb-recent-post-widget-list">
                    <?php while ( $posts_query->have_posts() ) : $posts_query->the_post(); ?>
                        <li id="post-<?php the_ID(); ?>">
                                <figure class="pb-recent-post-widget-thumbnail-section">
                                    <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                                        <?php the_post_thumbnail( 'widget-post-image' ); ?>
                                    </a>
                                </figure>

                            <div class="pb-recent-post-widget-section">
                                <h2 class="pb-recent-post-widget-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                                <?php if( pb_theme_options('enable_widget_meta') == '1' ) { ?>
                                    <div class="pb-recent-post-widget-meta clearfix">
                                        <ul>
                                        <?php if( pb_theme_options('widget_meta_date') == '1' ) { ?>
                                            <li><i class="fa fa-clock-o"></i><span><?php echo get_the_date(); ?></span></li>
                                            <?php } ?>
                                            <?php if( pb_theme_options('widget_meta_comments') == '1' ) { ?>
                                            <li><i class="fa fa-comments">
                                                <span>
                                                <?php printf( _nx( '', '%1$s', 'comments title', get_comments_number(), 'personalblog' ), number_format_i18n( get_comments_number() ) ); ?>
                                                </span>
                                              </i>
                                           </li>
                                           <?php } ?>
                                        </ul>
                                        <ul>
                                        <?php if( pb_theme_options('widget_meta_category') == '1' ) { ?>
                                            <li><p class="category"><i class="fa fa-folder-open"> </i> <?php the_category(', '); ?></p></li>
                                            <?php } ?>
                                        <?php if( pb_theme_options('widget_meta_likes') == '1' ) { ?>
                                           <li><?php echo getPostLikeLink( get_the_ID() )?> </li>
                                           <?php } ?>

                                        </ul>
                                    </div>
                                <?php } ?>
                            </div>
                        </li>
                    <?php endwhile; ?>
                </ul>
            </div>
        <?php }
        wp_reset_postdata();

        // Closing of widget

        echo $after_widget;
        }

        // Widget Form
        public function form( $instance ) {

            $defaults =  array(
            'title'         => 'Popular Posts',
            'popular_posts_order_by'        => 'post_views_count',
            'num_fetch'         => '5',
            'poular_posts_time_range'       => '0'

        );

        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
            <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e( 'Title:', 'strawberry' ); ?></label>
            <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( $instance['title'] ); ?>" />
        </p>

            <p>
                <label for="<?php echo esc_attr ($this->get_field_id('popular_posts_order_by')); ?>"><?php esc_html_e( 'Order by:', 'strawberry' ); ?></label>
                <select name="<?php echo esc_attr ($this->get_field_name('popular_posts_order_by')); ?>" id="<?php echo esc_attr ($this->get_field_id('popular_posts_order_by')); ?>" class="widefat">
                    <option value="post_views_count"<?php selected( $instance['popular_posts_order_by'], 'post_views_count' ); ?>><?php esc_html_e( 'View Count', 'strawberry' ); ?></option>
                    <option value="comment_count"<?php selected( $instance['popular_posts_order_by'], 'comment_count' ); ?>><?php esc_html_e( 'Most commented', 'strawberry' ); ?></option>
                </select>
           </p>

            <p>
                <label for="<?php echo esc_attr ($this->get_field_id('poular_posts_time_range')); ?>"><?php esc_html_e( 'Time Range:', 'strawberry' ); ?></label>
                <select name="<?php echo esc_attr ( $this->get_field_name('poular_posts_time_range')); ?>" id="<?php echo esc_attr ( $this->get_field_id('poular_posts_time_range')); ?>" class="widefat">
                    <option value="0"<?php selected( $instance['poular_posts_time_range'], '0' ); ?>><?php esc_html_e( 'All time', 'strawberry' ); ?></option>
                    <option value="1 year ago"<?php selected( $instance['poular_posts_time_range'], '1 year ago' ); ?>><?php _e( 'This year', 'strawberry' ); ?></option>
                    <option value="1 month ago"<?php selected( $instance['poular_posts_time_range'], '1 month ago' ); ?>><?php esc_html_e( 'This month', 'strawberry' ); ?></option>
                    <option value="1 week ago"<?php selected( $instance['poular_posts_time_range'], '1 week ago' ); ?>><?php esc_html_e( 'This week', 'strawberry' ); ?></option>
                    <option value="1 day ago"<?php selected( $instance['poular_posts_time_range'], '1 day ago' ); ?>><?php esc_html_e( 'Past 24 hours', 'strawberry' ); ?></option>
                </select>
            </p>
            <p>
            <label for="<?php echo $this->get_field_id('num_fetch'); ?>"><?php esc_html_e( 'Number of posts to show:', 'strawberry' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('num_fetch'); ?>" name="<?php echo $this->get_field_name('num_fetch'); ?>" type="text" value="<?php echo intval( $instance['num_fetch'] ); ?>" />
           </p>

            <?php
        }

        // Update the widget
        public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['popular_posts_order_by'] = $new_instance['popular_posts_order_by'];
        $instance['poular_posts_time_range'] =$new_instance['poular_posts_time_range'];
        $instance['num_fetch'] = $new_instance['num_fetch'];
        return $instance;
    }



    }
    ?>

Thanks

1
  • The notice is referring to line 26 of your code, may you show the line of code that it's referring to? The popular_posts_order_by shows up a few times in your code. – Ethan O'Sullivan Aug 26 '16 at 18:49
4

This notice appears when you try to access an undefined index of an array.

To fix this, you'll need to check if the index exists before you access it. For this you can use isset() or array_key_exists():

I'd need to see the exact line of code that the notice is referring to (line 26) in order to provide you with an an exact answer to your code. Here's a general example:

// Options 1: isset()
$value = isset($array['my_index']) ? $array['my_index'] : '';
// Options 2: array_key_exists()
$value = array_key_exists('my_index', $array) ? $array['my_index'] : '';
2
  • 2
    ps: then there's the PHP 7 version: $value = $array['my_index'] ?? ''; using the null coalesce operator ;-) – birgire Aug 26 '16 at 19:07
  • 26 line: <code> ' $popular_posts_order_by = $instance['popular_posts_order_by']; '</code> – bdtheme Aug 27 '16 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.