Skip Navigation

Passing Arguments to Views in WordPress

You can create one View and use it in different places, by controlling its output using arguments.

Let’s say you created a View that displays houses. In your site, houses have a custom checkbox field called “Available” that determines their availability on the market.

You can create one View to display both available and “taken” houses simply by passing a URL argument to it.

So, a user gets a list of “taken” houses when visiting the following URL:

  • example.com/houses_page/?available=0

And they get a list of available houses when visiting this URL:

  • example.com/houses_page/?available=1

As you can see, it’s the same page, only the “available” argument in the URL is different. This argument tells the View on that page how to filter the content.

How to add filtering arguments to a View

You can use arguments to control a View output by:

  • Custom fields
  • Taxonomies
  • Post filters
  • Post relationships

To add an argument to a View you need to use a Query Filter. Select your View block in block navigation and in the right sidebar, expand the Content Selection section. Click Add a filter and choose the filter you want to use. 

Adding a Query Filter to a View

Filtering by custom fields

When you select a filter by custom fields the following dialog for selecting the filter’s options appears.

Dialog for customizing a custom field filter

Use the first two dropdown menus to select the type of the data you want to compare (string,number, date, etc.) and the comparison function you need. 

In the third dropdown, select the URL parameter option and type in the parameter’s name you want to use. This means that the value is not hard-coded into the View, but will come from the page’s URL. 

Filtering by taxonomies

You can also filter a View by a specific taxonomy using arguments. When you add a filter by taxonomy, the following dialog appears.

Customizing a Taxonomy filter

In our example, we selected the “Gym Types” taxonomy.

Use the following steps to configure the URL parameter option:

  1. In the first dropdown, select the Set by one URL parameter option.
  2. In the second dropdown, select if you want to compare your taxonomy’s slug or a name.
  3. In the third dropdown, select how to include or exclude the compared values.
  4. Finally, set the name of your URL parameter. You can use the suggested one or put in a custom one.

Filtering by Post filters

You can add different types of Post filters. For example, you can filter by an author, post ID, date, and more. 

If you select to filter by author, you will see this box appear:

Customizing the author post filter

The URL parameter can be either the author ID or Username. In our example, we used the ID. 

The second input field lets you specify the name of the URL argument. This defaults to author-filter but you can customize it.

Filtering by Post relationships

You can also pass arguments to a View based on post relationships. You can find this option under Post filters.

Selecting the filter by post relationship

In the dialog that appears, select the post relationship you want to use. Then, set the option to check the post with an ID set by the URL parameter. If needed, you can also customize the name of this argument.

Filtering a View by a relationship filter