Last active
August 11, 2019 06:27
-
-
Save joshuadavidnelson/e2b8c57c800299d44ca4 to your computer and use it in GitHub Desktop.
Basic WP Query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A short template of an WP query (see Bill Erickson's example for all the arguments).