0

For the homepage WP_Query loop, I need to get the most recent 20 articles, specifically to display the page title, excerpt, category, and date. I am doing the following:

 $args = array(
    'post_type' => 'post',
    'posts_per_page' => 20,
    'update_post_term_cache' => false, // don't retrieve post terms
    'update_post_meta_cache' => false, // don't retrieve post meta
    'cat' => -2301, //exclude this category
    'orderby'       => 'post_date',
    'no_found_rows' => true, // counts posts, remove if pagination required           
 );

$the_query = new WP_Query( $args );

However, I am seeing a lot of DB queries like:

SELECT tr.object_id
FROM wp_term_relationships AS tr
INNER JOIN wp_term_taxonomy AS tt...

As well as

SELECT post_id, meta_key, meta_value
FROM wp_postmeta
WHERE post_id IN ....

Which uses up server resources, particular as there are 25'000 posts and 50'000 tags. Is there a more efficient method to run this query, given that I only need the fields as per above?

  • 3
    Why are you running a custom query on your homepage? – Pieter Goosen May 5 '15 at 18:49
  • 2
    You're doing expensive things, stuff like filtering out a category isn't super fast and requires a JOIN to check the taxonomy. You've even added pagination, followed by a parameter that has a comment saying remove if you need pagination. Are you sure you wouldn't be better off using the pre_get_posts filter to modify the existing query or caching the output? – Tom J Nowell May 5 '15 at 19:04
  • 2
    @TomJNowell I totally understand your point but I don't agree with you about the critic about the added pagination and added a parameter to avoid pagination; as far I know post_per_page is the only way to set the amount of posts you want to get, even if you don't want pagination like this case. Combining post_per_page and no_found_rows is the only way to set the limit amount of posts to get and avoid the counting of total rows for pagination; so combining both is good if you don't want pagination. (nopaging parameter set to true shows all posts ignoring post_per_page parameter). – cybmeta May 6 '15 at 6:33
  • you should look into persistent object caching solutions @josh – birgire May 6 '15 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.