How to Query Custom Parameters in WordPress

in PHP/Tutorials & Samples/Wordpress

One of the things that makes developing on WordPress so fun is that it is such a simple platform. A little while back, I had to add custom query parameters for a WordPress project. I then re-used the same approach in my portfolio section to breakdown my projects by category. So, how do you query custom parameters in WordPress? You simply use the PHP standard _GET variable. Below is a simple example of how to query custom parameters in WordPress, based on what I am using on this site.

Query for Custom Parameter

In the case of my portfolio page, I wanted to be able to pass a parameter to the portfolio page to tell the page what kind of category to filter by. So, I created a custom parameter called “filter”. I could then set the paramter inside various links such as:

  • http://miftyisbored.com/portfolio/?filter=websites
  • http://miftyisbored.com/portfolio/?filter=apps
  • http://miftyisbored.com/portfolio/?filter=illustrations
  • http://miftyisbored.com/portfolio/?filter=etc…

Then, in my template file for my portfolio page, I add the following code

        if(isset( $_GET["filter"] )){
	$filter_type =  $_GET["filter"];
	if($filter_type == "websites"){
	    // do stuff related to websites
	}else if($filter_type == "apps"){
            // do stuff related to apps
	}else{
            // use the fallback case...
        }

That’s all you need to query custom parameters in WordPress. Please note that this only works from within a template file. It will not work inside a wordpress post or a wordpress page.

Tags:

Mifty Yusuf is a Montreal-based software developer who enjoys playing with new web technologies as well as comic books and illustrations. He beleives that, no matter what the question is, the answer is always Batman!

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest from PHP

Go to Top