Skip to content

Instantly share code, notes, and snippets.

@danott
Created April 7, 2011 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danott/908465 to your computer and use it in GitHub Desktop.
Save danott/908465 to your computer and use it in GitHub Desktop.
Wordpress custom query for getting information outside of The Loop
<?
// This is a sub query for pulling data from outside of the loop..
// However, we need to preserve the original $wp_query if this is inside an existing loop.
// If this is the last query, then you can ditch it.
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('your-custom-query');
while ($wp_query->have_posts()) : $wp_query->the_post();
endwhile;
// if this is going before the main loop,
// we need to set the query back to the original.
// if not, ditch it.
$wp_query = $temp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment