WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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? – 40XUserNotFound 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

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:

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

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

Your Answer


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.