Skip to content

Instantly share code, notes, and snippets.

@guillermorangel
Created July 31, 2013 20:46
Show Gist options
  • Save guillermorangel/6125994 to your computer and use it in GitHub Desktop.
Save guillermorangel/6125994 to your computer and use it in GitHub Desktop.
WP_Query
<?php
// http://codex.wordpress.org/Class_Reference/WP_Query
$query = new WP_Query( 'p=7' );
$query = new WP_Query( 'name=about-my-life' );
$query = new WP_Query( 'page_id=7' );
$query = new WP_Query( 'pagename=contact' );
$query = new WP_Query( array( 'post_type' => 'page', 'post__in' => array( 2, 5, 12, 14, 20 ) ) );
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
/* The 2nd Query (without global var) */
$query2 = new WP_Query( $args2 );
// The 2nd Loop
while( $query2->have_posts() ):
$query2->next_post();
echo '<li>' . get_the_title( $query2->post->ID ) . '</li>';
endwhile;
// Restore original Post Data
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment