Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active August 11, 2019 06:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshuadavidnelson/e2b8c57c800299d44ca4 to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/e2b8c57c800299d44ca4 to your computer and use it in GitHub Desktop.
Basic WP Query
<?php
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'posts_per_page' => 100, // don't use -1, pick something reasonable
'no_found_rows' => true, // useful when pagination is not needed.
'update_post_meta_cache' => false, // useful when post meta will not be utilized.
'update_post_term_cache' => false, // useful when taxonomy terms will not be utilized.
// 'ignore_sticky_posts' => true, // ignore sticky posts
// 'fields' => 'ids', // only good if all you need is ids
'meta_query' => array( //(array) - Custom field parameters (available with Version 3.1).
array(
'key' => '_thumbnail_id', // only grab posts with featured images
'compare' => 'EXISTS',
),
),
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// Do Stuff
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
@joshuadavidnelson
Copy link
Author

A short template of an WP query (see Bill Erickson's example for all the arguments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment