如何防止执行默认查询,同时保留在模板中使用WP_QUERY的能力?

时间:2013-04-30 作者:Gniewomir Świechowski

PROBLEM: I\'am trying to prevent execute of default WordPress query for custom category template我正在尝试阻止执行自定义类别模板的默认WordPress查询。我找到了一个可能的解决方案,但看起来它阻止了所有post查询的执行:

function _cancel_query( $query ) {
    if ( !is_admin() && !is_feed() && is_search() ) {
        $query = false;
    }
    return $query;
}
add_action( \'posts_request\', \'_cancel_query\' );
资料来源:http://vadimk.com/2010/05/11/disable-wordpress-search-query/

“@query->is\\u main\\u query()”不能用于“posts\\u request”操作。有没有什么优雅的方法可以防止只使用默认的WordPress查询,允许我的代码处理帖子,而不执行无用的操作?

WHY: 我必须获得特色帖子,然后是固定数量的没有重复内容的普通帖子,这意味着帖子查询依赖于特色查询结果,因为无法保证在帖子查询中我找到足够的特色帖子来填充特色框。毕竟,我必须添加AJAX分页,但仍然没有重复的内容

EDIT #2: 如果您尝试按照Rarst建议在“pre\\u get\\u posts”中执行WP\\u查询(How to prevent execution of default query, while preserving ability to use WP_Query in template?):

pre\\u get\\u posts在设置WP\\u Query之前运行。某些依赖于WP\\u Query的模板标记和条件函数将不起作用。例如,is\\u front\\u page()将不起作用,而is\\u home()将起作用。在这种情况下,您需要直接使用查询变量,这些变量将作为参数传递给pre\\u get\\u posts挂钩(本页示例中的查询)

资料来源:https://codex.wordpress.org/Class_Reference/WP_Query

WP\\U查询将在此阶段在主查询之前初始化,为了避免出现这样的结果(签出日志),您必须将其包装:

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 个回复
最合适的回答,由SO网友:Rarst 整理而成

完全取消主查询是相当疯狂的,这涉及到子类化wp

我会:

钩住pre_get_posts 具有is_main_query() 检查运行特色查询(仍在钩子内)并将结果存储在某个地方使用这些结果在主查询上设置排除的帖子

SO网友:Mateusz Brandys

我知道回答可能有点晚了,但我在做测试项目时遇到了类似的问题。我是这样解决的。

/* 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 );
@更新:

将代码更改为使用普通函数而不是匿名函数,并使made code返回“false”而不是“SELECT 0 AS ID”,因为它导致返回一个伪空WP\\u Post对象。返回“false”会使$wpdb->get\\u results()提前退出,因此不会进行查询。

结束

相关推荐

get page templates

我有这个问题,我需要获得所有页面模板。我知道有很多方法可以根据他们的名字得到他们。我知道我可以包含一个页面模板,但我正在尝试将它们包含在索引页面的循环中。我刚刚安装了2012主题,修改了标题以包含我的脚本。我使用了Milo提供的一个功能,将所有帖子和页面加载到正在运行的主页中。现在,我需要包括所有模板,甚至那些我不知道其名称的模板。在抄本中,我发现了一个函数get_page_templates() 这似乎不再有效了。它给出了这个片段 <?php $templates = get_p