Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active June 13, 2019 17:54
Show Gist options
  • Save jrobinsonc/665b2a7f79aa7acc32b9c89758d03c83 to your computer and use it in GitHub Desktop.
Save jrobinsonc/665b2a7f79aa7acc32b9c89758d03c83 to your computer and use it in GitHub Desktop.
Wordpress snippet: WP_Query with performance in mind
<?php
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 4,
'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', // useful when only the post IDs are needed
]);
if ($query->have_posts()):
while ($query->have_posts()) : $query->the_post();
printf('<a class="post-title" href="%s">%s</a>', get_permalink(), get_the_title());
endwhile;
wp_reset_postdata();
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment