admin管理员组文章数量:1291349
I'm trying to apply a Datefilter for specific categories.
Wordpress crashs with a out of memory error. Is this a server problem or is my query wrong? At first I tried query -> (...) but with approach I get an error, cause of too few arguments. While using query = new WP_Query... I get the out of memory error.
Thank you for your help.
My code:
function test_filter( $query ) {
$options = get_option('dh_options');
$cats = "54,55,56";
if ($options['checkbox_usr_q'] == true) {
if ( !is_admin() && $query->is_main_query() && $query->in_category($cats)) {
$args = array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => 'May 1st, 2021',
'inclusive' => true
),
),
'posts_per_page' => -1
);
$query = new WP_Query( $args );
}
}
}
add_action( 'pre_get_posts', 'test_filter' , 100, 3);
Error Message:
Fatal error: Out of memory (allocated 699400192) (tried to allocate 262144 bytes) in /homepages/1/d861373325/htdocs/wp/wp-includes/class-wp-query.php on line 892
Fatal error: Out of memory (allocated 699400192) (tried to allocate 262144 bytes) in /homepages/1/d861373325/htdocs/wp/wp-includes/class-wp-recovery-mode.php on line 361
I'm trying to apply a Datefilter for specific categories.
Wordpress crashs with a out of memory error. Is this a server problem or is my query wrong? At first I tried query -> (...) but with approach I get an error, cause of too few arguments. While using query = new WP_Query... I get the out of memory error.
Thank you for your help.
My code:
function test_filter( $query ) {
$options = get_option('dh_options');
$cats = "54,55,56";
if ($options['checkbox_usr_q'] == true) {
if ( !is_admin() && $query->is_main_query() && $query->in_category($cats)) {
$args = array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => 'May 1st, 2021',
'inclusive' => true
),
),
'posts_per_page' => -1
);
$query = new WP_Query( $args );
}
}
}
add_action( 'pre_get_posts', 'test_filter' , 100, 3);
Error Message:
Fatal error: Out of memory (allocated 699400192) (tried to allocate 262144 bytes) in /homepages/1/d861373325/htdocs/wp/wp-includes/class-wp-query.php on line 892
Fatal error: Out of memory (allocated 699400192) (tried to allocate 262144 bytes) in /homepages/1/d861373325/htdocs/wp/wp-includes/class-wp-recovery-mode.php on line 361
Share
Improve this question
asked May 29, 2021 at 11:30
BennyVenassiBennyVenassi
31 bronze badge
1
|
1 Answer
Reset to default 1You're not using pre_get_posts
correctly. You've called new WP_Query
which then runs your hook, which calls new WP_Query
, which then runs your hook... and so on. You're in an infinite loop.
The proper way to use pre_get_posts
is to modify the $query
parameter using $query->set()
to adjust the query. You don't need to perform a brand new query:
function test_filter( $query ) {
$options = get_option( 'dh_options' );
$cats = array( 54, 55, 56 );
if ( $options['checkbox_usr_q'] == true ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_category( $cats ) ) {
$query->set(
'date_query',
array(
array(
'column' => 'post_date_gmt',
'after' => 'May 1st, 2021',
'inclusive' => true,
),
)
);
}
}
}
Also note that I changed $query->in_category
, which doesn't exist, to $query->is_category
, and I changed the value to an array of IDs. That function does not accept a comma-separated string.
本文标签: wp querydatequery in pregetposts out of memory
版权声明:本文标题:wp query - date_query in pre_get_posts out of memory 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741530976a2383750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'posts_per_page' => -1
is not recommended, you should set an upper limit, even if it's a limit you never expect to reach, e.g 500 to ensure that you never try to load more posts than your server is capable of loading, if only for performance reasons – Tom J Nowell ♦ Commented May 29, 2021 at 19:03