admin管理员组文章数量:1130349
I'm trying to set posts_per_page depending on what category is being shown.
The following code works.
function category_posts_per_page( $query )
{
if ( $query->is_main_query() && $query->is_category() && $query->is_category('books') ) {
$query->set('posts_per_page', 2);
}
}
add_action( 'pre_get_posts', 'category_posts_per_page' );
The problem:
I'm getting a php notice every 30 seconds in my debug log. No notices from visiting any posts, pages, categories or anything manually.
From debug.log:
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'term_id' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3502
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'name' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3504
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'slug' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3506
I've disabled all plugins, switched themes to a default one (and added the above code to functions.php).
How would I fix this? The code works and it's just a php notice, but it's a bit annoying... especially now in development and all notices are visible in debug.
I'm trying to set posts_per_page depending on what category is being shown.
The following code works.
function category_posts_per_page( $query )
{
if ( $query->is_main_query() && $query->is_category() && $query->is_category('books') ) {
$query->set('posts_per_page', 2);
}
}
add_action( 'pre_get_posts', 'category_posts_per_page' );
The problem:
I'm getting a php notice every 30 seconds in my debug log. No notices from visiting any posts, pages, categories or anything manually.
From debug.log:
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'term_id' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3502
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'name' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3504
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'slug' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3506
I've disabled all plugins, switched themes to a default one (and added the above code to functions.php).
How would I fix this? The code works and it's just a php notice, but it's a bit annoying... especially now in development and all notices are visible in debug.
Share Improve this question edited Oct 31, 2018 at 11:21 beholder asked Oct 31, 2018 at 10:36 beholderbeholder 235 bronze badges2 Answers
Reset to default 3is_category('category-name') won't work until after the query is run. If we check source code where those notices are generated, we can see that it's using get_queried_object, which gets populated with the results of the main query, and at the pre_get_posts stage will still be empty.
As an alternate, check the contents of $query->query_vars, which will be an array of query vars parsed from the requested URL.
Maybe you should try this behavior happens only on frontend, with adding !is_admin like this. It is maybe due to the admin ajax heartbeat?
function category_posts_per_page( $query )
{
if (!is_admin() && $query->is_main_query() && $query->is_category() && $query->is_category('books') ) {
$query->set('posts_per_page', 2);
}
}
add_action( 'pre_get_posts', 'category_posts_per_page' );
I'm trying to set posts_per_page depending on what category is being shown.
The following code works.
function category_posts_per_page( $query )
{
if ( $query->is_main_query() && $query->is_category() && $query->is_category('books') ) {
$query->set('posts_per_page', 2);
}
}
add_action( 'pre_get_posts', 'category_posts_per_page' );
The problem:
I'm getting a php notice every 30 seconds in my debug log. No notices from visiting any posts, pages, categories or anything manually.
From debug.log:
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'term_id' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3502
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'name' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3504
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'slug' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3506
I've disabled all plugins, switched themes to a default one (and added the above code to functions.php).
How would I fix this? The code works and it's just a php notice, but it's a bit annoying... especially now in development and all notices are visible in debug.
I'm trying to set posts_per_page depending on what category is being shown.
The following code works.
function category_posts_per_page( $query )
{
if ( $query->is_main_query() && $query->is_category() && $query->is_category('books') ) {
$query->set('posts_per_page', 2);
}
}
add_action( 'pre_get_posts', 'category_posts_per_page' );
The problem:
I'm getting a php notice every 30 seconds in my debug log. No notices from visiting any posts, pages, categories or anything manually.
From debug.log:
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'term_id' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3502
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'name' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3504
[31-Oct-2018 10:18:46 UTC] PHP Notice: Trying to get property 'slug' of non-object in /var/www/html/wp-includes/class-wp-query.php on line 3506
I've disabled all plugins, switched themes to a default one (and added the above code to functions.php).
How would I fix this? The code works and it's just a php notice, but it's a bit annoying... especially now in development and all notices are visible in debug.
Share Improve this question edited Oct 31, 2018 at 11:21 beholder asked Oct 31, 2018 at 10:36 beholderbeholder 235 bronze badges2 Answers
Reset to default 3is_category('category-name') won't work until after the query is run. If we check source code where those notices are generated, we can see that it's using get_queried_object, which gets populated with the results of the main query, and at the pre_get_posts stage will still be empty.
As an alternate, check the contents of $query->query_vars, which will be an array of query vars parsed from the requested URL.
Maybe you should try this behavior happens only on frontend, with adding !is_admin like this. It is maybe due to the admin ajax heartbeat?
function category_posts_per_page( $query )
{
if (!is_admin() && $query->is_main_query() && $query->is_category() && $query->is_category('books') ) {
$query->set('posts_per_page', 2);
}
}
add_action( 'pre_get_posts', 'category_posts_per_page' );
本文标签: categoriesiscategory in pregetposts results in php notices
版权声明:本文标题:categories - is_category in pre_get_posts results in php notices 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749217160a2334352.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论