Skip Navigation

[Resolved] Getting a list of Taxonomies used in Child Views

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

Problem: I have a View of child posts displayed on a parent post page. I would like to show a list of all the terms from a specific taxonomy that are associated with all the child posts in the View.

Solution:
Create a View of child posts filtered by post relationship, where the parent post is set by the current page. Insert this View into the parent post's Content Template.

Create a View of the taxonomy filtered by term ID, where the term is set by the current post in the loop. In the Loop output editor, use the following code to produce a comma-separated list of term IDs:

<wpv-loop>
      [wpv-item index=1]
       [wpv-taxonomy-id]
      [wpv-item index=other]
      ,[wpv-taxonomy-id]
</wpv-loop>

Add this code to your functions.php file to strip out all the extra markup from these results:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 
function prefix_clean_view_output( $out, $id ) {
  $ids = array( 12345 );
  if ( in_array( $id, $ids )) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Replace 12345 with the numeric ID of this View of Taxonomy.

Then create a second View of the taxonomy, filtered by term ID, provided by a shortcode attribute "terms". In the Loop Output of this View, include whatever you want to display for each term. There will be no duplicates in this View, even though the filter contains duplicate IDs.

Finally, nest the first View in the terms shortcode attribute of the second View:

[wpv-view name="second-view-slug" terms="[wpv-view name='first-view-slug']"]
This support ticket is created 6 years, 8 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 3 voices.

Last updated by Christian Cox 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#632875

I have the following situation:

I'm trying to build something like a multi shop catalogue with a list of shops and a shop page with a list of products from that particular shop.

So I have Set up the Shops as parent and the products as a child view, which all works great.

The products can be in different taxonomies like - shoes, tools, cars and so on

Now I'm somehow trying to get a View of taxonomies available only in this child view to display them in a widget.

As an example shop one has just products in the shoes and tool categories but not in cars, then the list should only return shoes and tools.

I have tried to set up a view for taxonomies which returns all taxonomies from products but got stuck in filtering them to return only the taxonomies for that particular shop.

What would be the best way to do this ?

Any hint is very welcome, thanks in advance ?

#634379

The problem here is that Widgets are not directly related to the post and will not work nicely when you want to pass Post Data to them so to filter a View by that Post Data.

As far I understand you have Several Posts (Shops) and each of those Shops has Child Posts (Products) where you assigned Taxonomy Terms to.
Now you want to display the Terms related to each Shop's Products on the Shop Page.

That's relatively simple in a View where you query the Post Type in question (child) and pass a Query Filter like:

Select posts that are children of the Post where this View is shown.

The Loop could include a link to the terms or a View, which in turn returns Terms that are set by a ShortCode attribute ID.

But, all this will not satisfy your need, because it will do all that once each item found in the loop.
You will end up with several repeating terms outputted.

What you need sounds like a TagCloud, which is something you cannot create out of the box with Toolset like that.
You could, however, create a Custom Search that functions like a TagCloud.
In the example shown above of displaying a View that returns posts who's parent is the current page, you can add a Custom filter to that view which when submitted leads to a results page - where you list the posts with those terms.

Does this help to achieve the goal?
Here is some documentation that will show in depth how to do the above:
https://toolset.com/documentation/user-guides/front-page-filters/
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/
https://toolset.com/documentation/user-guides/filtering-posts-by-user-selected-taxonomy/

#635501

Hi Beda

Thanks for your reply - looks like that this question pushed Toolset at its present state to it's limits.

I was tinkering around a bit and noticed that in the child view results I get the categories linked with each product, but of course this can cause some duplicates as you mentioned.

Do you think it would be possible somehow to just produce a child view which returns a list of the categories without duplicates - similar to a "group by" in SQL ? This might be a challenge for your dev team 🙂

Best regards, Peter

#636727

Hi Peter, Beda is away for the day so I will follow up here. One way you can eliminate duplicates is to use two Views. The first View is the one you have already created, but instead of outputting a link or the term name in each result, output the taxonomy term ID. Use the following Loop Output code to produce a comma-separated list of taxonomy term IDs:

<wpv-loop>
      [wpv-item index=1]
       [wpv-taxonomy-id]
      [wpv-item index=other]
      ,[wpv-taxonomy-id]
</wpv-loop>

When you place this on the page, you should see a list of term IDs like 234, 345, 234, 567... including some duplicates. Next, add some custom code that strips out all the extra formatting markup from this View so we can use it as a filter in another View. Add the following code to your child theme's functions.php file:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
  $ids = array( 12345 );
  if ( in_array( $id, $ids )) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

Replace 12345 in the code above with the numeric ID of this View.

Then create a second View of the taxonomy, filtered by term ID, provided by a shortcode attribute "terms". In the Loop Output of this View, include whatever you want to display for each term. There will be no duplicates in this View, even though the filter contains duplicate IDs.

Finally, nest the first View in the terms shortcode attribute of the second View:

[wpv-view name="second-view-slug" terms="[wpv-view name='first-view-slug']"]