试图在wordpress中获取非对象错误

On the page with id 239 I am trying to hide a certain post-format from displaying on it, however I am getting this error on all pages:

"Trying to get property of non-object in C:\wamp\www\wordpress\wp-includes\query.php on line 4337"

This is the code I am using in the functions.php file:

function exclude_campaigns( $query ) {
     if( $query->is_main_query() && $query is_page(239) ) {
        $tax_query = array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 'post-format-link' ),
            'operator' => 'NOT IN',
        ) );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'exclude_campaigns' );

After removing the $query before the is_page check, it works on my example :(

Anyway, the error indicates some problem with the is_page() function. You could try to replace that check:

if( $query->is_main_query() && ! empty( $query->posts ) && $query->posts[0]->ID == 239 ) {
    // ...
}