Remove query arg single key value

I’m adding multiple values of the same key as a query string with add_query_arg function to filter products.

The problem is that i want to remove only the value of the link clicked instead of the whole key array.

For example when i click these 2 links:

http://example.com/?marca=amd
http://example.com/?marca=intel

The url will be like this:

http://example.com/?marca=amd&marca=intel

But when i click again in one of those links the query string is removed completetely instead of just remove the clicked value.

$marca_arg = isset($marca) && is_array($marca)
? $marca : [];

foreach ($marcas_terms as $marca_term) {
<a href=”<?php echo (in_array($marca_term->slug, $marca_arg)) ?
esc_url(remove_query_arg(‘marca’)) : esc_url(add_query_arg(‘marca[]’,
“$marca_term->slug”)); ?>” <?php echo (in_array($marca_term->slug,
$marca_arg)) ? “class=’selected'” : ” ?>><?php echo $marca_term->name;
?></a>
}

Read more here:: Remove query arg single key value

Leave a Reply

Your email address will not be published. Required fields are marked *