jrobinsonc
8/26/2016 - 3:57 AM

Wordpress snippet: WP_Query with performance in mind

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;