WordPress WP_Query() Not working properly

We would like to know that how we occur error when we implement this code in our sidebar.php

$categories = get_categories(); 
foreach($categories as $category) 
{ 
  printf('<h2>%s</h2><ul>', $category->cat_name);
  $posts = new WP_Query('cat='.$category->cat_ID);
  while($posts->have_posts())
  {     
    $posts->the_post();
    echo '<li>', the_title(), '</li>'; 
  }   
  print '</ul>';  
}

The error we are getting:

Read More

Fatal error: Cannot use object of type WP_Query as array in C:xampphtdocswordpresswp-includesquery.php on line 2374

Related posts

Leave a Reply

2 comments

  1. a possible conflict with the name $posts which is used by wp core;
    try, for instance:

    $categories = get_categories();  
    foreach($categories as $category)  
    {    
      printf('<h2>%s</h2><ul>', $category->cat_name);   
      $cat_posts = new WP_Query('cat='.$category->cat_ID);   
      while($cat_posts->have_posts())   
      {          
        $cat_posts->the_post();     
        echo '<li>', the_title(), '</li>';    
      }      
      print '</ul>';   
    }