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);
    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

  • Which line is 2374? Is your code being used in the loop? – 40XUserNotFound Jun 30 '11 at 5:59
  • yes its working fine . but its not showing the Post data and giving the error mentioned above – PPS 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()" . – PPS 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; }" – PPS Jun 30 '11 at 7:57

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

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

  • that's right. this solved my similar problem where I was using $pages as my variable. Some names might be reserved for wordpress. – Saw-mon and Natalie Aug 14 '17 at 1:05

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:

  • Giving error when we placed it at the end. "Fatal error: Call to undefined function wp_reset_postdata()" – PPS Jun 30 '11 at 7:32

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.