remove_query_arg()WP 1.5.0

Removes an item or items from a query string.

1 time — 0.000055 sec (very fast) | 50000 times — 0.19 sec (very fast) | PHP 7.1.5, WP 4.8.2

No Hooks.

Return

String. New URL query string.

Usage

remove_query_arg( $key, $query );
$key(string|string[]) (required)
Query key or keys to remove.
$query(false|string)
When false uses the current URL.
Default: false

Examples

1

#1 Example of deleting URL parameters

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

$arr_params = array( 'details', 'type', 'date' );

echo esc_url( remove_query_arg( $arr_params ) );
// output: http://www.example.com/client/

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

0

#2 Deleting several request parameters from the URL

Suppose we pass the query parameters remove_recomend and post_id, process them and then we need a link without these parameters to use it further in the code:

// uses $_SERVER['REQUEST_URI'] and the passed query parameters

// delete the parameters that are no longer needed
$page_url = remove_query_arg( [ 'remove_recomend', 'post_id' ] );

// use the $page_url variable cleared of parameters

echo esc_url( $page_url );
0

#3 Remove request parameters from the specified URL

$url = 'http://blog.example.com/2009/?hello=world&foo=bar';
echo remove_query_arg( 'hello', $url );
//> http://blog.example.com/2009/?foo=bar

Changelog

Since 1.5.0 Introduced.

Code of remove_query_arg() WP 6.0

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 );
}