Title: $instance variable checking null difficulty
Last modified: August 21, 2016

---

# $instance variable checking null difficulty

 *  [jaehee0113](https://wordpress.org/support/users/jaehee0113/)
 * (@jaehee0113)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/instance-variable-checking-null-difficulty/)
 * Hello, I am currently developing a widget plugin and I am getting an input from
   user and that input is stored in `$instance['code']`
 * I would like to use this as argument to restrict the number of posts shown on
   the main website. However, if the input is not provided, then I want to set the
   default as 10 (so display 10 posts).
 * It worked fine when I used form as I use isset function to determine whether 
   the input was null but now, in widget function that does not work and I want 
   to know how can I achieve this.
 * Here is part of my code (I tested that the variable behaves correctly!)
 *     ```
       public function widget($args, $instance) 
   
       	{
   
       		extract($args, EXTR_SKIP);
   
       		//global WP theme-driven "before widget" code
       		echo $before_widget;
   
       		// code before your user input
       		//echo '<iframe width="560" height="315" src="//www.youtube.com/embed/';
   
       		//echo $instance['code'];
   
       		// code after your user input
       		//echo '"frameborder="0" allowfullscreen></iframe>';
   
       		if($instance['code'] == null){
       			$numPosts = 10;
       		}else{
       			$numPosts = $instance['code'];
       		}
   
       		$cateName = $instance['title'];
   
       		$category_id = get_cat_ID($cateName);
       		$catquery = new WP_Query( 'cat=' .$category_id);
   
       		if($catquery->have_posts()):
       			while($numPosts > 0 && $catquery->have_posts()) : $catquery->the_post();
       			$numPosts -= 1;?>
       			<article class="post">
       			<h2> <a href="<?php the_permalink(); ?>"> <?php the_title();?></a> </h2>
       			<?php the_post_thumbnail(array(20,20)); ?>
       			written by <?php  the_author(); ?>
       			<h3 id="comments"> <?php comments_number('no responses', 'one response', '% responses'); ?> </h3>
       			<?php the_content(); ?>
       			</article>
       			<?php endwhile;
   
       		else :
       			echo '<p> No content found </p>';
   
       		endif; 
   
       		wp_reset_query();
   
       		f//global WP theme-driven "after widget" code
       		echo $after_widget;
       	}
       ```
   
 *     ```
       if($instance['code'] == null){
       			$numPosts = 10;
       		}else{
       			$numPosts = $instance['code'];
       		}
       ```
   
 * This part is the intention but does not work. Can anyone help?

Viewing 1 replies (of 1 total)

 *  Thread Starter [jaehee0113](https://wordpress.org/support/users/jaehee0113/)
 * (@jaehee0113)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/instance-variable-checking-null-difficulty/#post-5081575)
 * Sorry there was a type there it works perfectly now 🙂

Viewing 1 replies (of 1 total)

The topic ‘$instance variable checking null difficulty’ is closed to new replies.

## Tags

 * [check](https://wordpress.org/support/topic-tag/check/)
 * [instance](https://wordpress.org/support/topic-tag/instance/)
 * [NULL](https://wordpress.org/support/topic-tag/null/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 1 reply
 * 1 participant
 * Last reply from: [jaehee0113](https://wordpress.org/support/users/jaehee0113/)
 * Last activity: [11 years, 10 months ago](https://wordpress.org/support/topic/instance-variable-checking-null-difficulty/#post-5081575)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
