Skip to main content

I while back I posted how to use custom post variables in your wordpress site and still user permalinks.

Alot of you commented and asked how to get 2 variables working. Well its about time. Here is the code:

If you havent done this before I suggest you read Part 1 which you can find here: WordPress permalinks and custom $_GET Variables : Part 1

ok…

First of all add your second variable to be passed in the “add_query_var” function, so instead of:

   // old line passing one variable
   $aVars[] = "sid";

  // new line passing more than one
  array_push('sid','var2')

then in the “add_rewrite_rules” function you have to change two things.

Change the Regular Expression to look for the second variable in the url. for example:

   // old line looking for one variable
   $aNewRules = array('permalink-test/([^/]+)/?$' => 'index.php?pagename=permalink-test&sid=$matches[1]');

   // now add the following regex: ([^/]+)/
   // and also add your new variable to the end: &var2=$matches[2]
   // so the new line will be:
   $aNewRules = array('permalink-test/([^/]+)/([^/]+)/?$' => 'index.php?pagename=permalink-test&sid=$matches[1]&var2=$matches[2]');

and that is it.

Dont forget to flush the rules! Easy way to do this is go to your wordpress admin Settings->Permalinks then hit save changes.

To use the variables in your theme template or plugin you can just use:

$gVar = $wp_query->query_vars['sid'];
$gVar1 = $wp_query->query_vars['var2'];

echo $gVar;
echo $gVar1;

Thats it.. I hope this helps! If you have questions… find the comment box below! 🙂

Cheers

Janes Oosthuizen

Author Janes Oosthuizen

Programmer and Tech Junky from Cape Town, South-Africa. I have been programming for more than 15 years in various languages including ( CSS, HTML, javascript, PHP, MySQL, Wordpress and many other ).

More posts by Janes Oosthuizen

Join the discussion 9 Comments

  • Troglos says:

    I still am not able to pass the 2nd variable.

    Here what I wrote in functions.php:

    function add_query_vars($aVars) {
    array_push($aVars, ‘myvar’, ‘mycat’);
    return $aVars;
    }
    add_filter(‘query_vars’, ‘add_query_vars’);

    function add_rewrite_rules($aRules) {
    $aNewRules = array(‘news/([^/]+)/([^/]+)/?$’ => ‘index.php?pagename=news&myvar=$matches[1]&mycat=$matches[2]’);
    $aRules = $aNewRules + $aRules;
    return $aRules;
    }
    add_filter(‘rewrite_rules_array’, ‘add_rewrite_rules’);

    and this in the page:

    echo $gVar = $wp_query->query_vars[‘myvar’];
    echo $gVar1 = $wp_query->query_vars[‘mycat’];

    I can print just the $gVar

    what did I do wrong?

  • David says:

    thank you janes.

    i’ll try it tonite.

    amazing tutorial. with this i can easily use another php script/cms and still use wordpress permalink structure.

    btw, to flush permalink, you don’t need to save the permalink. just visit permalink settings page will do.

    and you can use query var in template useing get_query_var() function.

    http://codex.wordpress.org/Function_Reference/get_query_var

    • Janes Oosthuizen says:

      Thanks for the kind words David. been away but back in full swing now. Thanks for the tip, will keep that in mind!

  • Daniel says:

    Thanks for this, Janes! This is something that I’ve been searching a solution for quite some time now.

    My only remaining problem is that now that I have two vars going through, only one wont work. So I’d like the second var to be optional. If its there one, use it. If not, pass only the first var. Feels like I should be squeezing in a ? somewhere?

    And a little side note, I believe that your “array_push” example should be like this:
    array_push($aVars,’sid’,’var2′);

    It looks like that on the previous page. 🙂

    Thanks again!

    • Daniel says:

      Solved it by adding another rule with only one var.
      I’m guessing that it could and would be better to solve it with RegEx, but this work for the time being.

      • Janes Oosthuizen says:

        Hey Daniel. Was just about to post that, lol! Thanks for answering it for me. btw, had a look at the fromupnorth.com site. VERY NICE!! bookmarked it.. will def visit it often! Cheers.

        • Daniel says:

          Haha! 🙂

          Thanks, glad your diggin’ it! Working on a custom image archive to make it easier to browse the approx. 16.000 categorized images I’ve published over the years. And I can finally continue the work now that I found out how to add new rules, so thanks again! 🙂

Leave a Reply