remove_query_arg ( string|array $key, bool|string $query = false )

Removes an item or list from the query string.

Return: (string) New URL query string.

Source file: wp-includes/functions.php

View source


Parameters

$key

(string|array) (Required) Query key or keys to remove.

$query

(bool|string) (Optional) When false uses the $_SERVER value.

Default value: false


Explanation

Usage:

// individual parameter
esc_url( remove_query_arg( $key, $query ) );

// multiple parameters in an array
esc_url( remove_query_arg( array('$key1', 'key2', 'key3'), $query ) );

Used by

Uses


Source

function remove_query_arg( $key, $query = false ) {
	if ( is_array( $key ) ) { // removing multiple keys
		foreach ( $key as $k )
			$query = add_query_arg( $k, false, $query );
		return $query;
	}
	return add_query_arg( $key, false, $query );
}


Changelog


User Contributed Notes

  1. Assuming we’re at the WordPress URL “http://www.example.com/client/?details=value1&type=value2&date=value3″…

    Note the use of esc_url() before outputting the link.

    // This would output '/client/?type=value2&date=value3'
    echo esc_url( remove_query_arg( 'details' ) );
    
    // This would output '/client/'
    $arr_params = array( 'details', 'type', 'date');
    echo esc_url( remove_query_arg( $arr_params ) );
    
  2. When you want to manipulate a URL that is not of the page your script is in, add the targeted URL in the second parameter as below. The use of esc_url() is not required here (though encouraged), because the value is known to be safe:

    // This would output 'http://www.example.com/2014/03/11/'
    echo esc_url( remove_query_arg( 'details',  'http://www.example.com/2014/03/11/?details=value1' ) );
    
    // This would output 'http://www.example.com/2014/03/11/?type=value2&date=value3'
    echo esc_url( remove_query_arg( 'details',  'http://www.example.com/2014/03/11/?details=value1&type=value2&date=value3' ) );
    
    // This would output 'http://www.example.com/2014/03/11/'
    $arr_params = array( 'details', 'type', 'date');
    echo esc_url( remove_query_arg( $arr_params, 'http://www.example.com/2014/03/11/?details=value1&type=value2&date=value3' ) );