categories - is_category in pre_get_posts results in php notices

admin2025-06-06  8

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 badges
Add a comment  | 

2 Answers 2

Reset to default 3

is_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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749217186a317344.html

最新回复(0)