Add new query variable to wordpress url

Today, I just want to share with you an useful tip, this will help you add new query variable to wordpress url. To understand what i mean, please read following example.

For an example:

I have an url like this

http://www.adminblog.com/products/product-a/

Then i want to add some custom queries which i want use for my purposes as loading a custom page, redirecting to new location, display a popup, etc …

http://www.adminblog.com/products/product-a/?price=1000

New query: price

Now, How to add new query:

Add following function to functions.php file of your template (locates at root of template folder)

add_filter('query_vars', 'add_my_var'); function add_my_var($qvars) { $qvars[] = 'some_unique_vars'; return $qvars; }

If you want to get your custom query within your template, just do this

$var=get_query_var('my_var'); /* Do some thing with $var */

Sure, you can use $_GET, $_REQUEST to get your variables but by using query_var function, you officially register that query with wordpress core, so you can integrate your query into more functions and let it work properly.

Done.Hope this short tip will be useful for your projects.

Written on 24 May 2012

Share your thoughts