Skip to main content
1 of 5
birgire
  • 67.8k
  • 7
  • 119
  • 251

I assume you're hooking into the pre_get_posts action.

Note that there's no is_admin() method of the \WP_Query class.

But the interesting part is that PHP will not complain about it,

$query->is_admin()

it will run but always return false. This is the reason why:

/**
 * Make private/protected methods readable for backwards compatibility.
 *
 * @since 4.0.0
 * @access public
 *
 * @param callable $name      Method to call.
 * @param array    $arguments Arguments to pass when calling.
 * @return mixed|false Return value of the callback, false otherwise.
 */
 public function __call( $name, $arguments ) {
        if ( in_array( $name, $this->compat_methods ) ) {
                return call_user_func_array( array( $this, $name ), $arguments );
        }
        return false;
}

We can use the is_admin() check:

add_action( 'pre_get_posts', function( \WP_QUery $query )
{
    if( is_admin() )
        return;

    // ... your stuff here ...
    
} );

or we could use the is_admin attribute of the \WP_QUery instance:

add_action( 'pre_get_posts', function( \WP_QUery $query )
{
    if( $query->is_admin )
        return;

    // ... your stuff here ...
    
} );
birgire
  • 67.8k
  • 7
  • 119
  • 251