VẤN ĐỀ: Tôi đang cố gắng ngăn việc thực hiện truy vấn WordPress mặc định cho mẫu danh mục tùy chỉnh. Tôi đã tìm thấy một giải pháp khả thi, nhưng có vẻ như nó ngăn chặn việc thực hiện tất cả các truy vấn bài viết:
function _cancel_query( $query ) {
if ( !is_admin() && !is_feed() && is_search() ) {
$query = false;
}
return $query;
}
add_action( 'posts_request', '_cancel_query' );
Nguồn: http://vadimk.com/2010/05/11/disable-wordpress-search-query/
'@ query-> is_main_query ()' sẽ không hoạt động với hành động 'Post_Vquest'. Có cách nào tao nhã để ngăn chặn truy vấn WordPress mặc định CHỈ, cho phép mã của tôi xử lý việc nhận bài đăng mà không thực hiện các hoạt động vô ích?
TẠI SAO: Tôi phải nhận được các bài đăng nổi bật, sau đó cố định số lượng bài đăng bình thường mà không có nội dung trùng lặp, ngụ ý rằng truy vấn bài đăng dựa trên kết quả truy vấn nổi bật, vì không có gì đảm bảo rằng trong truy vấn bài đăng tôi tìm thấy đủ các bài đăng nổi bật để điền vào hộp đặc trưng. Và sau tất cả, tôi sẽ phải thêm phân trang AJAX - vẫn không có nội dung trùng lặp.
EDIT # 2: Nếu bạn cố gắng thực hiện WP_Query trong 'pre_get_posts' như Rarst đã đề xuất ( Cách ngăn thực hiện truy vấn mặc định, trong khi duy trì khả năng sử dụng WP_Query trong mẫu? ):
"pre_get_posts chạy trước khi WP_Query được thiết lập. Một số thẻ mẫu và các hàm điều kiện dựa trên WP_Query sẽ không hoạt động. Ví dụ, is_front_page () sẽ không hoạt động, mặc dù is_home () sẽ hoạt động trực tiếp. với các vars truy vấn, được chuyển đến hook pre_get_posts làm đối số ($ truy vấn trong ví dụ trên trang này). "
Nguồn: https://codex.wordpress.org/Class_Reference/WP_Query
WP_Query sẽ được khởi tạo ở giai đoạn này, trước truy vấn chính và để tránh kết quả như thế này (kiểm tra nhật ký), bạn phải bọc nó:
if ($query->is_main_query()) {
$args = array();
$query = new WP_Query( $args );
}
ĐĂNG NHẬP:
[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