• When adding an array to the global query variables with the query_vars hook, it is always flattened to the string ‘Array’.

    I remember seeing a ticket on trac about this, but now I can’t seem to find it.

    Can anyone shed any light, is there a workaround?

    ["query_vars"]=>
      array(1) {
        ["multi-cat"]=>
        string(5) "Array"
     }

    Update: There’s a ticket here that explains all values are cast as strings for security.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter wdm

    (@wdm)

    Here is the code:

    function add_query_vars($query_vars) {
      $query_vars[] = 'multi-cat';
      return $query_vars;
    }

    With the url blog.com/?multi-cat[]=6&multi-cat[]=10

    (GET vars come from a multiple category widget I’m working on, the selects are each named ‘multi-cat[]’ to create an array of category IDs).

    function parse_query_vars($query) {
      var_dump($query);
    }

    The resulting var_dump shows the array is recognised, but cast to a string:

    ["query_vars"]=>
      array(1) {
        ["multi-cat"]=>
        string(5) "Array"
     }
    Thread Starter wdm

    (@wdm)

    There is a kind-of workaround, by explicitly adding the query_vars from $_GET:

    function parse_query_vars($query) {
    
      if ( isset($_GET['multi-cat']) ) {
    
        $query->query_vars['multi-cat'] = $_GET['multi-cat'];
    
        var_dump($query);
    
      }
    
    }

    The var_dump now reports:

    ["query_vars"]=>
      array(1) {
        ["multi-cat"]=>
        array(2) {
          [0]=>
          string(1) "6"
          [1]=>
          string(2) "10"
        }
      }

    Unfortunately custom rewrite rules don’t seem to work with this method.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Pass array to query_vars’ is closed to new replies.