How to prevent WordPress from retrieving data for the last 10 posts in the database?

According to the Query Monitor plugin, WordPress is calling four queries for the last 10 posts in the database, starting with this to know the IDs:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type="post"
AND (wp_posts.post_status="publish"
OR wp_posts.post_status="private")
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10

The below and two other will then follow:

SELECT wp_posts.*
FROM wp_posts
WHERE ID IN (29,27,26,25,24,23,22,21,20,18)

I’m using a custom template so I don’t need this, is there a way to disable it?

1 Answer
1

The reason is you are visiting the home page and it contains the WordPress Loop like below to query 10 recent posts.

<?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        // Your loop code
    endwhile;
else :
    // not found template
endif;
?>

The number of posts can be managed from settings->reading->Blog pages show at most. from WordPress dashboard.
There you will see 10 posts selected in the value. if you want to display 5 posts then change this value.

if you want to remove this query from home page then edit index.php, frontpage.php or home.php file depending upon your theme. Recommended way is to create a page template and do your thing there. how to create page template in WordPress

Leave a Comment