Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to know why we get an 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 is:

Fatal error: Cannot use object of type WP_Query as array in C:\xampp\htdocs\wordpress\wp-includes\query.php on line 2374

share|improve this question

migrated from stackoverflow.com Jun 30 '11 at 7:43

This question came from our site for professional and enthusiast programmers.

    
Which line is 2374? Is your code being used in the loop? –  Cupcake Jun 30 '11 at 5:59
    
yes its working fine . but its not showing the Post data and giving the error mentioned above –  Rodger Jun 30 '11 at 6:20
    
Which line is 2374? –  Pekka 웃 Jun 30 '11 at 7:45
    
We Posted wp_reset_postdata(); at the end of the while loop, it the error changed to this one "Fatal error: Call to undefined function wp_reset_postdata()" . –  Rodger Jun 30 '11 at 7:53
    
on this function. "function next_post() { $this->current_post++; $this->post = $this->posts[$this->current_post]; "[comment] Line number 2374 here is the problem [/comment] " return $this->post; }" –  Rodger Jun 30 '11 at 7:57
show 1 more comment

2 Answers 2

sometimes, probably if you have two queries in one page, you need to reset the query data, try to insert the code below after while loop:

wp_reset_postdata();
share|improve this answer
    
Giving error when we placed it at the end. "Fatal error: Call to undefined function wp_reset_postdata()" –  Rodger Jun 30 '11 at 7:32
add comment

I was having a similar error when using a WP_Query as given at http://wordpress.stackexchange.com/a/42334/16898.

I replaced the variable name $posts with an arbitrary name $mposts and the error was gone.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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