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