title: array_pop() expects parameter 1 to be array in Wordpress related posts by taxonomy code
body: <blockquote>
  <p>Warning: array_pop() expects parameter 1 to be array, null given in ... on line 34</p>
  </blockquote>
  <p>I get the following error when using the following function. The output of the function works as expected except the error shows up before the output.</p>
  <pre><code>&lt;?php 
  global $post;
  
  //Get the terms for the current post
  $terms = get_the_terms( $post-&gt;ID , 'character', 'string');
  echo $terms[1];
  $potentials = array();
  $c = 0;
  $totalFound = 0;
  
  //Gather your posts for each term
  foreach($terms as $term){
     $q = array(
      'character' =&gt; $term-&gt;slug, //term to retrieve from custom taxonomy
      'numberposts' =&gt; 3,  //limit to 4 posts
      'post_type' =&gt; 'videos', //get only posts
      'exclude' =&gt; $post-&gt;ID //exclude current post
     );
     $posts = get_posts($q);
     $totalFound+= count($posts);
     $potentials[$c++] = array_reverse($posts);
  }
  
  $count = 0;  //The number of good posts we've found
  $index = 0;  //Number of posts we've tried
  $max = $totalFound &gt; 3 ? 3 : $totalFound;  //The max we can find
  $posts = array();
  
  //Now pick one post from each term until we reach our quota,
  //or have checked them all
  while($count &lt; $max){
  
    //Pop off a post to use
    $rpost = array_pop($potentials[$index++]);
  
    //if we got a post (if there was one left)
    if($rpost){
      //don't take duplicates
      if(!isset($posts[$rpost-&gt;ID])){
        $posts[$rpost-&gt;ID] = $rpost;
        $count++;
      }
    }
    $index = ($index % 3); //rotate through the 4 term arrays
  }
  foreach($posts as $post){
      setup_postdata($post);
      $exclusive = get('aoexclusive_yes');
      if(!$exclusive) {$exclusive = null;} else {$exclusive = 'exclusive';}
      $image = wp_get_attachment_image_src( get_post_thumbnail_id(  $post-&gt;ID ), &quot;video-thumb&quot; );
  ?&gt;
                      &lt;div class=&quot;thumb-post&lt;?php echo ' '.$exclusive; ?&gt;&quot;&gt;
  &lt;?php if($image) { ?&gt;
  
                          &lt;a class=&quot;featured-image&quot; href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;img src=&quot;&lt;?php echo $image[0]; ?&gt;&quot; /&gt;&lt;/a&gt;
  &lt;?php } else { ?&gt;
  
                          &lt;a class=&quot;featured-image&quot; href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;img src=&quot;&lt;?php bloginfo('stylesheet_directory'); ?&gt;/assets/images/default.jpg&quot; /&gt;&lt;/a&gt;
  &lt;?php } ?&gt;
                          &lt;h1&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;
  &lt;?php ao_post_meta(); ?&gt;    
  
                      &lt;/div&gt;
  &lt;?php } ?&gt;
  </code></pre>
  <p>Does anyone have any experience with this error or see what might be causing it?</p>
  <p>Code originally came from <a href="http://wordpress.org/support/topic/custom-taxonomy-related-posts-query" rel="nofollow noreferrer">http://wordpress.org/support/topic/custom-taxonomy-related-posts-query</a></p>