Skip to content

Instantly share code, notes, and snippets.

@MatRouault
Forked from joshuadavidnelson/basic-wp-query.php
Last active April 23, 2016 11:18
Show Gist options
  • Save MatRouault/62ba8170a5b5de6d559fad5a0fff11ba to your computer and use it in GitHub Desktop.
Save MatRouault/62ba8170a5b5de6d559fad5a0fff11ba 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.
// '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