Solution to Annoying WordPress Error Warning: Invalid argument supplied for foreach() wp-includes/query.php on line 2762

Initial post by Blackspotradish, this saved me hours of frustration!

This was an annoying bug on one of my wordpress + buddypress for a while and the fix is amazingly simple :

Edit your wp-includes/query.php around line 2762

// Always sanitize
foreach ( $this->posts as $i => $post ) {
    $this->posts[$i] = sanitize_post( $post, 'raw' );
}

and replace by :

// Always sanitize
if ( $this->post_count > 0 ) {
    foreach ( $this->posts as $i => $post ) {
        $this->posts[$i] = sanitize_post( $post, 'raw' );
    }
}

About Rafael (Rafi) Hecht
Rafi Hecht is a leading SEO Consultant and Web Programmer, employed full-time at iRISEmedia.com, an online marketing company, and currently residing in Toronto. He has been in the IT workforce since 2003, and has focused on Search Engine Optimization and Web programming since 2007. Rafi holds a Bachelor of Science in Management/Information Systems from Touro's Lander College for Men as well as a Master of Science degree in Information Systems at Stevens Institute of Technology. He also is a devoted husband and father. His personal blog can be found at http://blog.rafihecht.com.

%d bloggers like this: