Using WordPress ‘remove_query_arg()’ PHP function

The remove_query_arg() WordPress PHP function removes an item or items from a query string.

Usage

A generic example of using the function:

$new_url = remove_query_arg('key', $old_url);

Parameters

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

More information

See WordPress Developer Resources: remove_query_arg()

Examples

Removing a single query argument

Remove the ‘details’ argument from the current URL:

$new_url = remove_query_arg('details');
echo esc_url($new_url);

Removing multiple query arguments

Remove the ‘details’, ‘type’, and ‘date’ arguments from the current URL:

$keys_to_remove = array('details', 'type', 'date');
$new_url = remove_query_arg($keys_to_remove);
echo esc_url($new_url);

Removing a query argument from a specific URL

Remove the ‘details’ argument from a specific URL:

$old_url = 'http://www.example.com/page/?details=value1&type=value2&date=value3';
$new_url = remove_query_arg('details', $old_url);
echo esc_url($new_url);

Removing multiple query arguments from a specific URL

Remove the ‘details’, ‘type’, and ‘date’ arguments from a specific URL:

$old_url = 'http://www.example.com/page/?details=value1&type=value2&date=value3';
$keys_to_remove = array('details', 'type', 'date');
$new_url = remove_query_arg($keys_to_remove, $old_url);
echo esc_url($new_url);

Removing all query arguments from the current URL

Remove all query arguments from the current URL:

$new_url = strtok($_SERVER["REQUEST_URI"], '?');
echo esc_url($new_url);