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.

6 thoughts on “Add new query variable to wordpress url

  1. At this time it sounds like Movable Type is the preferred blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?

  2. ITIL therefore sets the guidelines for all management of IT infrastructure including management of networks, management of data centre as well as local operations. Having the fortitude to fight on in life, despite sometimes unseen but enormous obstacles, is a gift that good people enjoy, no matter what their income, and they should have our respect. It is a fun business, it is very profitable, and has made me a better person.

  3. Excellent goods from you, man. I’ve take into accout your stuff previous to and you’re simply too wonderful. I actually like what you have got here, certainly like what you’re stating and the way in which in which you are saying it. You’re making it enjoyable and you continue to care for to keep it sensible. I can not wait to learn far more from you. This is actually a tremendous site.

  4. What’s up everybody, here every one is sharing such know-how, thus it’s nice to read this website, and I used to visit this website daily.

  5. An experienced business mentor has been there and done that. Most women who escape from situations like I did do not ever tell about it. You do not have to give a lot of money to some world famous Internet guru in order to obtain decent advice.

  6. Attractive component to content. I just stumbled upon your weblog and in accession capital to claim that I get in fact loved account your blog posts. Any way I’ll be subscribing to your augment or even I success you get entry to constantly quickly.|

Leave a Reply

Your email address will not be published. Required fields are marked *