WordPress – How to pass variable as parameter in URL

 

This simple method worked for my WordPress website to pass a parameter in URL. In this tutorial, the Datewise Invoices page lists down all the invoices created between the dates. When you click on a specific Invoice No, the invoice no is passed as parameter value and a new webpage opens up more details of the invoice.

 

Step 1: Add this code to your theme’s functions.php file

function add_query_vars_filter( $vars )
{
$vars[] = “parvar”;
return $vars;
}
add_filter( ‘query_vars’, ‘add_query_vars_filter’ );

 

Step 2: Now parvar variable is available in all WordPress pages for you to query and show the results.

if (isset($wp_query->query_vars[‘parvar’]))
{
$invoiceno = $wp_query->query_vars[‘parvar’];
}