Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a function that sets posts_per_page option depending on the category viewed.

I discovered and error which I can't really understand: first this is how I get the error:

function custom_posts_per_page($query) {

    if ( !is_admin() && is_category(4)) {

    // Leaving this empty or with content

    }

}

add_filter( 'pre_get_posts', 'custom_posts_per_page' );

So I get an error there if WP_Debug is turn to true and when I access a non-exixting category. So if I enter http://localhost/zz/category/uncategorized there is no problem but if I enter for example http://localhost/zz/category/aaaaaaaa (Category aaaaaaaa doesn't exist), It gets correctly to the 404 page but throws these errors:

Notice:  Trying to get property of non-object in C:\xampp\htdocs\zz\wp-includes\query.php on line 3420
Notice:  Trying to get property of non-object in C:\xampp\htdocs\zz\wp-includes\query.php on line 3422
Notice:  Trying to get property of non-object in C:\xampp\htdocs\zz\wp-includes\query.php on line 3424

What is wrong?

share|improve this question
    
Might this just be a result of not using quotes? Like this is_category( '4' ). –  ialocin Dec 1 '13 at 15:24
    
is_category( '4' ) , is_category( 'house' ) throws the same errors –  Alvaro Dec 1 '13 at 15:44
    
The errors only appear if I check is_category() in the pre_get_posts –  Alvaro Dec 1 '13 at 15:45

1 Answer 1

up vote 7 down vote accepted

After a bit of investigation...

If you pass a category to is_category it uses get_queried_object to grab data-- see the source. get_queried_object returns NULL for categories that do not exist. You can demonstrate that with:

function custom_posts_per_page($query) {
  var_dump(get_queried_object());
}
add_filter( 'pre_get_posts', 'custom_posts_per_page' );

Load a valid category archive and then an invalid one.

The is_category method does not check to see whether it has a valid object before trying to use that object, hence the error. I would definitely consider that a bug.

The only way I see around it is to check for the existence of the category first:

function custom_posts_per_page($query) {

    if ( !is_admin() && term_exists(4,'category') && $query->is_category(4)) {

    // Leaving this empty or with content

    }

}

add_filter( 'pre_get_posts', 'custom_posts_per_page' );

Or handle the logic yourself:

function custom_posts_per_page($query) {

    if ( !is_admin() && $query->is_category()) {
      $qobj = get_queried_object();
      if (!empty($qobj)
        && (isset($qobj->taxonomy) && 'category' == $qobj->taxonomy && 4 == $qobj->term_id)
      ) {
        var_dump($qobj);
      }
    // Leaving this empty or with content

    }

}

add_filter( 'pre_get_posts', 'custom_posts_per_page' );

Barely tested. Possibly buggy. Caveat emptor. No refunds.

share|improve this answer
2  
in your 2nd example, it should be $query->is_category(4), or you'll modify every query on the page. –  Milo Dec 1 '13 at 15:52
    
Fixed, thanks @Milo. Now I have to look over it again to see if I screwed up anything else :) –  s_ha_dum Dec 1 '13 at 15:54
    
+1 I was just looking at the source too, I guess that's what can be done. Or live with the message, because it isn't a real problem. –  ialocin Dec 1 '13 at 15:54
2  
You're of course absolutely right. Only thing that makes me wonder is, why isn't the check part of the core code. –  ialocin Dec 1 '13 at 16:11
1  
@ialocin : just a bug I think. I am going to check trac for this. It is a very simple patch. –  s_ha_dum Dec 1 '13 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.