Sometimes you made need a View whose output is the same as an existing View and which differs only by some query setting or filter used to generate the list of content.
The simplest way to vary the output of an existing View is to override its settings when you insert it. If you use the Fields and Views button to insert a View you have options to override limit & offset and ordering options for this instance.
You may want to display projects with a given status, e.g. “submitted”, or “archived”, where these are possible values of a “status” taxonomy.
You could create two Views to display projects using a Query Filter for status, one with a fixed value of “submitted”, and another with a fixed value of “archived”.
Or you could create a single View with a status filter where the value to filter by could be passed to the View as a variable. The View will return different results depending on the value passed.
You can either pass values to the View as a shortcode attribute where the View is inserted (e.g.
[wpv-view name="projects-by-status" wpvstatus="archived"]) or as a parameter appended to the page URL (e.g. site.com/projects/?wpvstatus=archived).
In this case the Query Filter expects to find a shortcode attribute wpvstatus that passes the required value. This attribute name (or a URL parameter name) is suggested by Views. You can change it, it just needs to match the attribute name where you insert the View.
An example use case for using shortcode attributes would be an overview page that lists all projects ordered by status. A limitation of WordPress (by design) is that you cannot order posts by taxonomy term. One way you might achieve this is with nested Views.
The outer View would be a taxonomy View that queries the taxonomy terms of the status taxonomy. In our example it would return four results, archived, consideration, pending, and submitted, and loop over them.
A crude example of the Loop Output section might look like the screenshot below, where we output the current status term title as a heading, then insert the nested View to display all projects with the status set by the shortcode attribute wpvstatus. In this example, the value of the attribute itself is dynamic, and will be the slug of the current status for each iteration of the outer View.
In our training site below we will show a simple example of re-using the same View, changing the limit setting to vary how many posts are displayed depending where the View is inserted.
So far in our training materials we have described inserting a View into a Layout using the View cell.
The View cell does not allow for overriding settings or passing values as shortcode attributes, and for these you will need to use a Visual Editor cell and then use the Fields and Views button to insert a View which you have previously created, overriding the settings as required.
That will insert a wpv-view shortcode which you can manually edit to add attributes if needed.
In our training site the front page is designed with a Content Layout that includes a View cell in the second row to display the next 3 upcoming events. If you edit the View cell you will see that it has a limit setting of 3.
We have a page “Upcoming events” which is currently empty where we want to display the same—events that have not yet happened—but we want to display all such events and not just the next 3.
- Edit the Upcoming Events page with the Layouts Editor
- Insert a Visual Editor cell in the first row
- Use the Fields and Views button to insert the Upcoming Events View (from the Post View section)
- In the Limit section enter -1 to override the existing setting of 3 and apply no limit
- Save and close the cell and the Layout and then visit the page on the front end to confirm the results
That will add the wpv-view shortcode to the cell like so
[wpv-view name="upcoming-events" limit="-1"]
Go to the Beyond the Basics support page and leave a comment. Nigel, the author of these pages, or one of the other Toolset team members will reply to you.