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:
The url will be like this:
But when i click again in one of those links the query string is removed completetely instead of just remove the clicked value.
function custom_query_vars_filter( $vars ) {
$vars[] = 'marca';
return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );
$marca = get_query_var('marca');
$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>
}
marca=and,intel
andexplode()
. Eg. example.com/?marca=amd,intel