Notice: Undefined index: suppress_filters

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.

Read More

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

Related posts

2 comments

  1. 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.

  2. 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.)

Comments are closed.