Skip to content

Instantly share code, notes, and snippets.

@krogsgard
Last active January 4, 2021 21:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krogsgard/5915715 to your computer and use it in GitHub Desktop.
Save krogsgard/5915715 to your computer and use it in GitHub Desktop.
sample custom WP_Query the right way
<?php
/*
* WP_Query happy dance
*
* this is a sample WP_Query the right way
*/
$args = array (
'post_type' => 'post',
'author' => 5 // these args can be whatever
);
$krogsquery = new WP_Query( $args );
if ( $krogsquery->have_posts() ) {
while ( $krogsquery->have_posts() ) {
$krogsquery->the_post();
// do stuff
}
} else {
// aww, no posts... do other stuff
}
wp_reset_postdata();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment