Skip Navigation

[Resolved] How to navigate hierarchical taxonomy to drill down to results

This thread is resolved. Here is a description of the problem and solution.

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 );
This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by rusty 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#600363

I am trying to make a sort of "drill-down" set of pages for users to find a store in their area. The idea here is that the user lands on a page that shows all the states we have stores in. They click a state to show all the cities we have stores in. They click a city to show a listing of the stores in that city and finally if they click on one of the stores listed, it will take them to that store's page.

There are 2 separate taxonomies involved: Cities and States.

All Stores Page --> Selected State Page --> Selected City Page --> Select Your Store --> Store Page

In other words:

All Stores --> Florida --> Tampa --> Tampa Stores --> Tampa Store #2

On the first page (Page), there is a Taxonomy View listing of the taxonomy "States", which shows all the states we have stores in.

The user clicks on a state and goes to a Taxonomy Archive shows all the cities in that state that have stores.

Let's say the user clicks on the state of Florida. This state has 4 stores. 2 stores in Tampa, and one in Orlando and one in Miami.

The cities are listed like so:

Tampa
Orlando
Tampa
Miami

Clicking on either instance of Tampa shows both stores in Tampa. The user can then select which store they would like to see. This is good. This is exactly what I want!

The problem is this: How do I get each city on the state page display only once, even if there are multiple stores in that city?

So instead of:

Tampa
Orlando
Tampa
Miami

I'd like to see:

Tampa (2)
Orlando
Miami

(Remember: This page is an archive.)

Is there a tutorial for building something like this? I have a feeling that using a taxonomy archive isn't going to work for this purpose. Please advise. Thanks!

#600442

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Daniel

I've given this a lot of thought—it is not easy to implement without writing custom queries—but while testing a solution that involves a minor bit of code to use a URL parameter in a taxonomy View I have run into a problem whereby the wpv_filter_taxonomy_query filter doesn't appear to be working correctly.

I'm consulting with colleagues and will get back to you.

#600451

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

location-taxonomy.png

OK, the wpv_filter_taxonomy_query filter wasn't being triggered because I need caching turned off for the View.

So, I couldn't think how you would do this with your set up how it is without resorting to writing custom queries, namely how you have a separate taxonomy for state and for city.

So I combined these into a single hierarchical Location taxonomy, with the top level the states, and the child terms the cities, as shown in the location-taxomomy screenshot.

I have a taxonomy View which lists just the States, which it does by adding a taxonomy parent filter to "Select taxonomy terms whose parent is None."

In the Loop Output section I link to a page which will display the Cities, and which passes the taxonomy term ID (of the State) as a URL parameter to that page.

The key part looks like this:

		<wpv-loop>
          <h3><a href="<em><u>hidden link</u></em>">[wpv-taxonomy-title]</a> ([wpv-taxonomy-post-count])</h3>
		</wpv-loop>

So I then create a second taxonomy View to show the cities which I add to a page "Cities of chosen state".

I again add a parent taxonomy Query Filter.

Taxonomy Views don't have the option to pass a value for the filter by URL parameter, so here I have to "hard-code" a State which I will then update with a little PHP. In my case I said the parent term was Florida.

My Loop Output section simply links to the taxonomy archive for the city (and includes the post count), like so:

		<wpv-loop>
          <h3>[wpv-taxonomy-link] ([wpv-taxonomy-post-count])</h3>
		</wpv-loop>

Now we need to fix the actual parent so that it is the State passed as a URL parameter from the previous page.

So I needed to add the following to my functions.php file:

/**
 * Modify tax query
 */
function custom_modify_tax_query( $tax_query_settings, $view_settings, $view_id ){

	if ( 9 == $view_id && isset($_GET['state']) ) {

		$tax_query_settings['child_of'] = $_GET['state'];
	}

	return $tax_query_settings;
}
add_filter( 'wpv_filter_taxonomy_query', 'custom_modify_tax_query', 101, 3 );

You will need to edit this for the correct View ID.

And, note that because the View uses a static choice for the parent State (e.g. Florida) and doesn't know we are changing it according to the URL parameter then the wpv_filter_taxonomy_query won't reliably be triggered. Where you insert this second View on the page you need to add the attribute cached="off".

Let me know how you get on.

#600576

Wow. This is an awesome answer! Thanks Nigel!

This will take a significant amount of re-working on my site. We have over 300 stores in 20 states! I know it's a long shot, but do you have a working demo where I can see exactly how it functions on the front end? I don't need a login, just want to see it in action. Thanks again!

#600590

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Daniel

I put it online, start here: hidden link

#600661

Works perfectly! Thanks again Nigel!