0

I have added this code in the TwentyFourteen theme's functions.php file.

function add_query_vars($aVars) {
    $aVars[] = "msds_pif_cat";
    return $aVars;
}
add_filter('query_vars', 'add_query_vars');

Then on the index.php file, I have added this:

$sMsdsCat = urldecode($wp_query->query_vars['msds_pif_cat']);
var_dump($sMsdsCat);

It does not return anything.

I have tried to print the $wp_query and it does not include that variable either. What I'm doing wrong?

  • Did you try get_query_var( 'msds_pif_cat' )? (it's a wrapper). What does your request look like? – birgire Jun 24 '14 at 11:56
  • it does not return anything either. I only added the above code in hte functions.php file and then the other code on the index.php file, so that is just a plain index url of WordPress installation. Could you explain what you mean by the request? – jay Jun 24 '14 at 12:04
  • So your request is like http://example.com/?msds_pif_cat=123 ? – birgire Jun 24 '14 at 12:17
  • ok great, what was the problem? – birgire Jun 24 '14 at 12:25
  • 1
    My mistake, I thought that the above function will automatically add the variable in the query and that would be accessible even on the plain URL without passing these parameters, but your comment gave me the idea that it would be only available if it is included in the request url. I could not accept your answer as it is a comment. – jay Jun 24 '14 at 12:32

Your Answer

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

Browse other questions tagged or ask your own question.