7

PROBLEM: I'am trying to prevent execution of default WordPress query for custom category template. I have found a possible solution, but looks like it preventing execution of all post queries:

function _cancel_query( $query ) {
    if ( !is_admin() && !is_feed() && is_search() ) {
        $query = false;
    }
    return $query;
}
add_action( 'posts_request', '_cancel_query' );

Source: http://vadimk.com/2010/05/11/disable-wordpress-search-query/

'@query->is_main_query()' won't work with 'posts_request' action. Is there any elegant way to prevent ONLY default WordPress query, allowing my code to handle getting posts, without performing useless operations?

WHY: I have to get featured posts, then fixed amount of normal posts without duplicate content, which implies that post query relies on featured query results, because there is no guarantee that in post query i find enough featured posts to populate featured box. And after all that i will have to add AJAX pagination - still without duplicate content.

EDIT #2: If you trying to perform WP_Query in 'pre_get_posts' as Rarst suggested (How to prevent execution of default query, while preserving ability to use WP_Query in template?):

"pre_get_posts runs before WP_Query has been setup. Some template tags and conditional functions that rely on WP_Query will not work. For example, is_front_page() will not work, although is_home()will work. In such cases, you will need to work directly with the query vars, which are passed to the pre_get_posts hook as an argument ($query in examples on this page)."

Source: https://codex.wordpress.org/Class_Reference/WP_Query

WP_Query will be initialized at this stage, before main query, and to avoid result like this ( check out log ) you have to wrap it :

if ($query->is_main_query()) {
    $args = array();
    $query = new WP_Query( $args );
}

LOG:

[30-Apr-2013 17:07:12 UTC] PHP Fatal error:  Maximum function nesting level of '100' reached, aborting! in C:\Users\serwis\Dropbox\www\XYZ\wp-includes\functions.php on line 2769

[30-Apr-2013 17:07:12 UTC] PHP Stack trace:

[30-Apr-2013 17:07:12 UTC] PHP   1. {main}() C:\Users\serwis\Dropbox\www\XYZ\index.php:0

[30-Apr-2013 17:07:12 UTC] PHP   2. require() C:\Users\serwis\Dropbox\www\XYZ\index.php:17

[30-Apr-2013 17:07:12 UTC] PHP   3. wp() C:\Users\serwis\Dropbox\www\XYZ\wp-blog-header.php:14

[30-Apr-2013 17:07:12 UTC] PHP   4. WP->main() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\functions.php:779

[30-Apr-2013 17:07:12 UTC] PHP   5. WP->query_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\class-wp.php:549

[30-Apr-2013 17:07:12 UTC] PHP   6. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\class-wp.php:485

[30-Apr-2013 17:07:12 UTC] PHP   7. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP   8. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP   9. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  10. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  11. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  12. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  13. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  14. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  15. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  16. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  17. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  18. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  19. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  20. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  21. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  22. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  23. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  24. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  25. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  26. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  27. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  28. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  29. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  30. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  31. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  32. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  33. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  34. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  35. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  36. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  37. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  38. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  39. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  40. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  41. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  42. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  43. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  44. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  45. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  46. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  47. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  48. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  49. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  50. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  51. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  52. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  53. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  54. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  55. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  56. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  57. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  58. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  59. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  60. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  61. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  62. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  63. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  64. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  65. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  66. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  67. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  68. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  69. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  70. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  71. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  72. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  73. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  74. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  75. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  76. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  77. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  78. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  79. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  80. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  81. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  82. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  83. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  84. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  85. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  86. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  87. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  88. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  89. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  90. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  91. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  92. do_action_ref_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  93. call_user_func_array() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  94. gs_pre_get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\plugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  95. WP_Query->__construct() C:\Users\serwis\Dropbox\www\XYZ\wp-content\themes\roots-XYZ\lib\custom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  96. WP_Query->query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  97. WP_Query->get_posts() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  98. WP_Query->parse_query() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1922

[30-Apr-2013 17:07:12 UTC] PHP  99. absint() C:\Users\serwis\Dropbox\www\XYZ\wp-includes\query.php:1442

2 Answers 2

5

Completely canceling main query is pretty much high level madness, that involves subclassing wp class.

I would:

  1. Hook into pre_get_posts with is_main_query() check
  2. Run featured query (still inside hook) and stash results somewhere
  3. Use those results to set excluded posts on main query
4
  • If i understand you correctly i cannot prevent WP from performing default query for template without messing up WP , BUT i can perform normal WP_query before main query is done, and everything will be OK? Can you explain to me why default query is so important, even if core WP mechanic (getting content) works before performing it? (i'am not very familiar with wp core, but willing to learn :) ) Commented Apr 30, 2013 at 15:54
  • Simply put main query is thoroughly glued to WP core load process. It is relatively easy to modify it, but trying to get rid of it completely is nasty and a technique that is never used in practice. It can be done, it's just not practical from development effort point of view. And yes - you can run custom queries independently, since they won't interfere with main one (except query_posts() one, which should not be used). Just be careful with is_main_query() checks so your changes apply to what it should and not all queries.
    – Rarst
    Commented Apr 30, 2013 at 15:58
  • Thanks for answer. It was really helpful. I will try your approach instead hacking WP :) Commented Apr 30, 2013 at 16:02
  • I'll put forth that since this comment was created in 2013 it's now more mid-level madness come 2016. A use-case for this behavior would be a front-end driven not by PHP/HTML templating but by JavaScript. If the needed bootstrap data does not match the default WP_Query then it can be "unnecessary" overhead for logged in users.
    – editor
    Commented Nov 24, 2015 at 20:18
9

I know it might be abit late for answer but I came across similar issue while making my test project. Here's how I solved it.

/* apply this filter only on relevant to you pages */
function mb_bail_main_wp_query( $sql, WP_Query $wpQuery ) {
    if ( $wpQuery->is_main_query() ) {
        /* prevent SELECT FOUND_ROWS() query*/
        $wpQuery->query_vars['no_found_rows'] = true;

        /* prevent post term and meta cache update queries */
        $wpQuery->query_vars['cache_results'] = false;

        return false;
    }
    return $sql;
}
add_filter( 'posts_request', 'mb_bail_main_wp_query', 10, 2 );

@UPDATE:

Changed code to use normal function instead of anonymous one and made made code return 'false' instead of 'SELECT 0 AS ID' as it caused to return one dummy empty WP_Post object. Returning 'false' makes $wpdb->get_results() to bail early so no query is made.

2
  • 4
    It's almost never too late, because the answers aren't just for the asker. Could you please add some additional explanation? There is a lot in there, for example, because of the anonymous function, this code needs PHP 5.3+. Commented May 23, 2014 at 19:05
  • nice find, note that the WP_Query object is already passed as a reference s specified in in the documentation.
    – Aurovrata
    Commented May 30, 2018 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.