问题描述

问题:我试图阻止执行自定义类别模板的默认 WordPress 查询。我找到了一个可能的解决方案,但是看起来像是阻止所有帖子查询的执行:

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_main_query()’ 不能与’posts_request’ 动作配合使用。有没有任何优雅的方式来防止只有默认的 WordPress 查询,允许我的代码处理获取帖子,而不执行无用的操作?

为什么:我必须得到精选帖子,然后固定数量的正常帖子没有重复的内容,这意味着后期查询依赖于特色查询结果,因为不能保证在后期查询我找到足够的特色帖子填充功能框。毕竟,我将不得不添加 AJAX 分页 – 仍然没有重复的内容。

编辑#2:如果您尝试以 Rarst 建议 (How to prevent execution of default query, while preserving ability to use WP_Query in template?) 在’pre_get_posts’ 中执行 WP_Query:

「pre_get_posts 运行在 WP_Query 已经被设置之前,依赖于 WP_Query 的一些模板标签和条件函数将不起作用,例如, is_front_page() 将不起作用,尽管 is_home() 将会工作,在这种情况下,您需要直接使用查询 vars 作为参数传递给 pre_get_posts 钩子 (在此页面的示例中为 $ query) 。

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

WP_Query 将在这个阶段进行初始化,在主查询之前,为避免这样的结果 (检出日志),你必须包装它:

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

日志:

[30-Apr-2013 17:07:12 UTC] PHP Fatal error:  Maximum function nesting level of '100' reached, aborting! in C:UsersserwisDropboxwwwXYZwp-includesfunctions.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:UsersserwisDropboxwwwXYZindex.php:0

[30-Apr-2013 17:07:12 UTC] PHP   2. require() C:UsersserwisDropboxwwwXYZindex.php:17

[30-Apr-2013 17:07:12 UTC] PHP   3. wp() C:UsersserwisDropboxwwwXYZwp-blog-header.php:14

[30-Apr-2013 17:07:12 UTC] PHP   4. WP->main() C:UsersserwisDropboxwwwXYZwp-includesfunctions.php:779

[30-Apr-2013 17:07:12 UTC] PHP   5. WP->query_posts() C:UsersserwisDropboxwwwXYZwp-includesclass-wp.php:549

[30-Apr-2013 17:07:12 UTC] PHP   6. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesclass-wp.php:485

[30-Apr-2013 17:07:12 UTC] PHP   7. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP   8. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP   9. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  10. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  11. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  12. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  13. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  14. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  15. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  16. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  17. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  18. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  19. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  20. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  21. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  22. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  23. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  24. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  25. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  26. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  27. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  28. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  29. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  30. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  31. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  32. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  33. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  34. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  35. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  36. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  37. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  38. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  39. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  40. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  41. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  42. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  43. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  44. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  45. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  46. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  47. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  48. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  49. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  50. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  51. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  52. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  53. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  54. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  55. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  56. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  57. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  58. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  59. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  60. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  61. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  62. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  63. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  64. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  65. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  66. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  67. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  68. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  69. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  70. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  71. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  72. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  73. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  74. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  75. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  76. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  77. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  78. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  79. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  80. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  81. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  82. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  83. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  84. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  85. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  86. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  87. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  88. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  89. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  90. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  91. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  92. do_action_ref_array() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1924

[30-Apr-2013 17:07:12 UTC] PHP  93. call_user_func_array() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  94. gs_pre_get_posts() C:UsersserwisDropboxwwwXYZwp-includesplugin.php:487

[30-Apr-2013 17:07:12 UTC] PHP  95. WP_Query->__construct() C:UsersserwisDropboxwwwXYZwp-contentthemesroots-XYZlibcustom.php:14

[30-Apr-2013 17:07:12 UTC] PHP  96. WP_Query->query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:3059

[30-Apr-2013 17:07:12 UTC] PHP  97. WP_Query->get_posts() C:UsersserwisDropboxwwwXYZwp-includesquery.php:2972

[30-Apr-2013 17:07:12 UTC] PHP  98. WP_Query->parse_query() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1922

[30-Apr-2013 17:07:12 UTC] PHP  99. absint() C:UsersserwisDropboxwwwXYZwp-includesquery.php:1442

最佳解决方案

完全取消主查询是几乎非常高级的疯狂,涉及 wp 类的子类化。

我会:

  1. is_main_query()检查卡入 pre_get_posts

  2. 运行特征查询 (仍然在钩子中) 并将结果存在某处

  3. 使用这些结果在主查询中设置排除的帖子

次佳解决方案

我知道答案可能迟到,但是在进行测试项目时遇到了类似的问题。这是我如何解决它。

/* 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:

更改代码以使用正常功能而不是匿名的,并使得代码返回’false’ 而不是 「SELECT 0 AS ID」,因为它返回一个虚拟的空 WP_Post 对象。返回’false’ 使 $ wpdb-> get_results() 提前保留,因此不会进行任何查询。

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。