Add a $default argument to get_query_var() and WP_Query::get(). fixes #16471.

git-svn-id: https://develop.svn.wordpress.org/trunk@27304 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2014-02-26 23:57:10 +00:00
parent 56c79f659c
commit 88f1b6e7ec
2 changed files with 20 additions and 7 deletions

View File

@ -19,12 +19,13 @@
* @uses $wp_query * @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 * @return mixed
*/ */
function get_query_var($var) { function get_query_var( $var, $default = '' ) {
global $wp_query; global $wp_query;
return $wp_query->get($var); return $wp_query->get( $var, $default );
} }
/** /**
@ -2130,13 +2131,15 @@ class WP_Query {
* @access public * @access public
* *
* @param string $query_var Query variable key. * @param string $query_var Query variable key.
* @param mixed $default Value to return if the query variable is not set. Default ''.
* @return mixed * @return mixed
*/ */
function get($query_var) { function get( $query_var, $default = '' ) {
if ( isset($this->query_vars[$query_var]) ) if ( isset( $this->query_vars[ $query_var ] ) ) {
return $this->query_vars[$query_var]; return $this->query_vars[ $query_var ];
}
return ''; return $default;
} }
/** /**

View File

@ -77,4 +77,14 @@ class Tests_Query extends WP_UnitTestCase {
$this->assertEquals( get_the_ID(), $post_id ); $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 ) );
}
} }