1

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>
  }
  • 1
    IMO you can use marca=and,intel and explode(). Eg. example.com/?marca=amd,intel – maheshwaghmare Mar 18 '18 at 17:44
  • The problem here isn't removing the query args, it's the action URL for your form. What does your form look like? – Jacob Peattie Mar 18 '18 at 23:54
  • @maheshwaghmare Could you provide a code example of that? I'm a php beginner – Gabriel Souza Mar 19 '18 at 1:03
  • @JacobPeattie I'm not really using a form, i think add_query_arg function doesn't require it – Gabriel Souza Mar 19 '18 at 1:06
2

[EDIT] This should work the way you wanted it to:

  $marca = get_query_var('marca');

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

  $url_base = remove_query_arg( 'marca' );
  $n = count( $marca_arg );

  foreach ($marcas_terms as $marca_term) {
    $selected = in_array( $marca_term->slug, $marca_arg );
    if ( $selected ) {
      $marca_arg2 = array_diff( $marca_arg, [ $marca_term->slug ] );

      // Rebuils the indexes. Just remove if not necessary.
      $marca_arg3 = [];
      foreach ( $marca_arg2 as $slug ) {
        $marca_arg3[] = $slug;
      }

      $url = add_query_arg( 'marca', $marca_arg3, $url_base );
      $marca_arg2 = $marca_arg3 = null;
    } else {
      $url = add_query_arg( 'marca[' . $n . ']', $marca_term->slug );
    }
  ?>
    <a href="<?php echo esc_url( $url ); ?>" <?php echo $selected ? // wrapped
    "class='selected'" : ''; ?>><?php echo $marca_term->name; ?></a>
  <?php
  } // End $marcas_terms loop.
  • With your code when clicking again in the link it won't remove that value from the url, also i need the marca variable to be an array of strings, not a single string separated by comma. – Gabriel Souza Mar 19 '18 at 14:18
  • Please try the updated code. – Sally CJ Mar 20 '18 at 1:19
  • This is not consistent, cause it keeps the "&" when removing the second or after value of the query string. And as i have other query vars theres no way to know the position of the vars in the query string. – Gabriel Souza Mar 20 '18 at 19:03
  • And how about the updated code? – Sally CJ Mar 21 '18 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.