I am using the below code in functions.php to add custom query variables to my WordPress project.

function add_custom_query_var( $vars ){
  $vars[] = "brand";
  return $vars;
add_filter( 'query_vars', 'add_custom_query_var' );

And then on the content page I am using:

<?php echo $currentbrand = get_query_var('brand'); ?>

So now I can pass a URL like:


and get nike in result.

How can I pass multiple values for a brand. So I would like to have 3 values for brand say, nike, adidas and woodland.

I have tried the following and none of them works:

  1. http://myexamplesite.com/store/?brand=nike&brand=adidas&brand=woodland It just returns the last brand, in this case woodland.

  2. http://myexamplesite.com/store/?brand=nike+adidas+woodland This returns all three with spaces. I kind of think this is not the correct solution. So may be there is a correct way to pass multiple values for a query variable and retrieve them in an array may be so that a loop can be run.


I am giving you some solutions whatever you want to use.

Using plus http://myexamplesite.com/store/?brand=nike+adidas+woodland

//Don't decode URL
    $brand = get_query_var('brand');
    $brand = explode('+',$brand);

using ,separator http://myexamplesite.com/store/?brand=nike,adidas,woodland

$brand = get_query_var('brand');
$brand = explode(',',$brand);

Serialize way

<?php $array = array('nike','adidas','woodland');?>
http://myexamplesite.com/store/?brand=<?php echo serialize($array)?>
to get url data
$array= unserialize($_GET['var']);
or $array= unserialize(get_query_var('brand'));

json way

<?php $array = array('nike','adidas','woodland');
  $tags = base64_encode(json_encode($array));
http://myexamplesite.com/store/?brand=<?php echo $tags;?>
<?php $json = json_decode(base64_decode($tags))?>
<?php print_r($json);?>

using - way

I suggest using mod_rewrite to rewrite like http://myexamplesite.com/store/brand/nike-adidas-woodland or if you want to use only php http://myexamplesite.com/store/?brand=tags=nike-adidas-woodland. Using the '-' makes it search engine friendly.

    $brand = get_query_var('brand');
    $brand = explode('-',$brand);

In every case, you will get the same result

        [0] => nike
        [1] => adidas
        [2] => woodland

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.