1

I'm doing some de-bugging on a theme I'm working on and I'm hoping someone can help me please.

I used this function that Justin Tadlock made to display custom post types on the blog page and with wp-debug set to true I get a Notice: Undefined index: suppress_filters message.

The code is as follows:

// Custom Post Type for the public blog posts to show on Index or blog page
add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

if ( ( is_home() && false == $query->query_vars['suppress_filters'] ) || is_feed() )
$query->set( 'post_type', array( 'news', 'attachment' ) );

return $query;
}

If anyone could help that would be great. Thanks

2

If $query->query_vars['suppress_filters'] is not set you will get that message.

Use empty($query->query_vars['suppress_filters']) instead of false == $query->query_vars['suppress_filters'] ) or use $query->get('suppress_filters') like this false == $query->get('suppress_filters').

Untested (minimally tested) but I believe either of those should give you the same results minus the notice.

| improve this answer | |
  • Ran into same issue, I used if ( is_home() && false == $query->get('suppress_filters')) which works perfectly. Thanks! – zigojacko Mar 24 '15 at 8:57
0

Sounds like $query->query_vars['suppress_filters'] isn't set. Try this:

// Custom Post Type for the public blog posts to show on Index or blog page
add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

    if ( 
        ( is_home() && (
            isset( $query->query_vars['suppress_filters'] ) && 
            false == $query->query_vars['suppress_filters']  
            )
        ) ||
        is_feed() 
    ) {
        $query->set( 'post_type', array( 'news', 'attachment' ) );
    }

    return $query;
}

I added a check to make sure that $query->query_vars['suppress_filters'] is actually set before you check to see if it's false. (Also I broke up the if() statement for easier reading. (I think I've matched up all the ( and ) correctly.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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