Is `query_posts` really slower than secondary query?

You ask:

is query_posts really slower than some secondary query…

The fact is that if you’re calling query_posts() from a theme then it already is a secondary query. WordPress has already queried the database once to get a certain page then it hits your query_posts() function and queries the database again creating a second query and overwriting the original request / data.

From a speed perspective you’re not going to notice any difference. The query_posts() functions uses WP_Query() and takes microseconds to overwrite some global variables. The biggest overhead will come from you having to normalize your data and make up for any niche issues with pagination.

Simply put, it’s all around inefficient. So, while there may not be a “big red button” that says “Don’t press here!” it’s pretty much implied that it shouldn’t be pressed. It’s not big and red but The Codex specifically states:

Note: This function isn’t meant to be used by plugins or themes.

If you want to be efficient and need to override the main query – use pre_get_posts which will modify the query parameters before it calls it from the database which saves an entire database call ( in that we don’t need a secondary query ).

Leave a Comment