diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index cf60255c1e..eed431d96e 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -18,13 +18,14 @@ * @since 1.5.0 * @uses $wp_query * - * @param string $var The variable key to retrieve. + * @param string $var The variable key to retrieve. + * @param mixed $default Value to return if the query variable is not set. Default ''. * @return mixed */ -function get_query_var($var) { +function get_query_var( $var, $default = '' ) { global $wp_query; - return $wp_query->get($var); + return $wp_query->get( $var, $default ); } /** @@ -2130,13 +2131,15 @@ class WP_Query { * @access public * * @param string $query_var Query variable key. + * @param mixed $default Value to return if the query variable is not set. Default ''. * @return mixed */ - function get($query_var) { - if ( isset($this->query_vars[$query_var]) ) - return $this->query_vars[$query_var]; + function get( $query_var, $default = '' ) { + if ( isset( $this->query_vars[ $query_var ] ) ) { + return $this->query_vars[ $query_var ]; + } - return ''; + return $default; } /** diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index bc16c1d7ad..18f23d83a6 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -77,4 +77,14 @@ class Tests_Query extends WP_UnitTestCase { $this->assertEquals( get_the_ID(), $post_id ); } } + + /** + * @ticket 16471 + */ + function test_default_query_var() { + $query = new WP_Query; + $this->assertEquals( '', $query->get( 'nonexistent' ) ); + $this->assertFalse( $query->get( 'nonexistent', false ) ); + $this->assertTrue( $query->get( 'nonexistent', true ) ); + } } \ No newline at end of file