Use WP_Query object inside a function

This is basic PHP. Just a note before I continue, never ever globalize variables. WordPress has already done quite a crappy job regarding this. Globalization is evil, because anyone and anything, knowingly or unknowingly can change a global variable. This makes globals a nightmare to debug. So in short, NEVER EVER globalize.

Whenever you need to pass something outside a anonymous function to the function, you should use the use() keyword. As example, you can try

function () use( $the_query )
{
    var_dump( $the_query );
}

EDIT:

In your code, you can do the following

fluid_grid( function() use ( $the_query )
{
    ?>    
    // Here goes the markup that should be printed out later   
    <?php
} ); 

Leave a Comment