Problem:
With a hierarchical taxonomy of States > Cities the client wants to be able to display the top level as a list, click one term to choose it, display the next level of the hierarchy in a list (including post counts), then click on a term to display the posts themselves.
Solution:
- Create a taxonomy View to display only the top-level terms by including a taxonomy parent filter to "Select taxonomy terms whose parent is None."
- In the Loop Output section link to a page where a second View will be added and add a URL parameter to pass the chosen State, like so:
<wpv-loop>
<h3><a href="http://mysite.com/cities-of-chosen-state/?state=[wpv-taxonomy-id]">[wpv-taxonomy-title]</a> ([wpv-taxonomy-post-count])</h3>
</wpv-loop>
- Now create a second taxonomy View to display the Cities for the chosen State set by the URL parameter. Add a parent taxonomy Query Filter.
For taxonomy Views it is not possible to set the value using a URL parameter, so set a fixed value for the time being. We'll overwrite this with a snippet of code.
In the Loop Output section link to the taxonomy archive for the city (and include the post count), like so:
<wpv-loop>
<h3>[wpv-taxonomy-link] ([wpv-taxonomy-post-count])</h3>
</wpv-loop
This View will be added to the page which is linked to from the first View. For this second View you must add the attribute cached="off" to ensure the filters are triggered.
We then need to add a snippet of PHP (to the theme's functions.php, or using a plugin such as Code Snippets) to modify the term value in the second View, like so:
/**
* Modify tax query
*/
function custom_modify_tax_query( $tax_query_settings, $view_settings, $view_id ){
if ( 9 == $view_id && isset($_GET['state']) ) { \\ Edit View ID
$tax_query_settings['child_of'] = $_GET['state'];
}
return $tax_query_settings;
}
add_filter( 'wpv_filter_taxonomy_query', 'custom_modify_tax_query', 101, 3 );