I feel there is a red button saying “Do not press” below query_posts(). For some time I am trying to demystify this phenomena with very low success to be honest.

For example, probable the main reason not to use this function is a broken pagination. Second, was it may be slow (read: calling twice), or there are better ways (pre_get_posts). There may be more reasons.

For those who may downwote this question from instinct, I would appeal, to hold at least for a week or so. There is always a second chance to do that. I am hardly waiting to see at least some answers, before a team pressure.

For those not read this post, I would say that this function is nor deprecated, nor it is marked as _doing_it_wrong, nor there are plans.
Even the ticket to make it deprecated has been closed recently.

Some may say they read this function must not be used within the WordPress. I am providing in here the full function.

File: /wp-includes/query.php
79: /**
80:  * Set up The Loop with query parameters.
81:  *
82:  * This will override the current WordPress Loop and shouldn't be used more than
83:  * once. This must not be used within the WordPress Loop.
84:  *
85:  * @since 1.5.0
86:  *
87:  * @global WP_Query $wp_query Global WP_Query instance.
88:  *
89:  * @param string $query
90:  * @return array List of posts
91:  */
92: function query_posts($query) {
93:     $GLOBALS['wp_query'] = new WP_Query();
94:     return $GLOBALS['wp_query']->query($query);
95: }

You can see This must not be used within the WordPress Loop., meaning not within the WordPress loop, implying that before the WordPress loop may be not prohibited.

I noted from this post that this function is not slower than any secondary query (having the same arguments).

Bottomline, speaking of the pagination query_posts is much better solution for the pagination than the get_posts since get_posts actually disables SQL_CALC_FOUND_ROWS.

Some good thoughts on the query_posts may be:

  • the query arguments are not limited, you can set anything you like.
  • you can even set the arguments to use pagination, if you don’t plan to use core pagination functions; super easy you can create your own pagination
  • in a typical blog only small portion of the pages, may use pagination. The most of the pages, in a majority of regular simple cases, don’t even use pagination so to do something quick and to reuse the main query templates (read loop) query_posts may be handy.

These are just a the few thoughts, I am sure there are more, but certainly If you know what you are downing you cannot be wrong.


Do no

3 Answers
3

query_posts() is useful in cases when there is no main query: calls to wp-admin/admin-ajax.php, wp-admin/admin-post.php or wp-login.php for example.

Yes, you can achieve the same results there without query_posts() and slightly less compact code instead. But if you don’t have to care about side effects, using query_posts() is acceptable.

Leave a Reply

Your email address will not be published. Required fields are marked *