$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - is_category in pre_get_posts results in php notices|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - is_category in pre_get_posts results in php notices

matteradmin10PV0评论

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' );
Post a comment

comment list (0)

  1. No comments so far