Skip Navigation

[Resolved] Taxonomy View showing terms applied to related posts

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

Problem: I have two custom post types - Characters and Photographs - linked in a post relationship. The Photographs CPT has a custom taxonomy Photoshoots, with terms for each Photoshoot assigned to each Photograph. In the template for Characters posts, I would like to display a list of all Photoshoots terms applied to all Photographs posts related to the current Character post.

Solution: The problem does not have a straightforward solution, because you need to filter a View of taxonomy terms by the properties of a related post type. The best solution requires you to use the legacy Views editor to create a nested View structure that produces the desired terms list.
See step-by-step instructions in the following comment: https://toolset.com/forums/topic/taxonomy-view-6/#post-2101763

This support ticket is created 3 years, 7 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)

Tagged: 

This topic contains 22 replies, has 2 voices.

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

Assisted by: Christian Cox.

Author
Posts
#2101311

Tell us what you are trying to do?
I have a CPT called Photograph and one called Character. Photograph has a Taxonomy called Photoshoot.
There is a relationship between Photograph and Character.

I am creating the Template for Character and would like to list all of the Photoshoots they were involved in. I am using LEGACY Views, NOT Block Views, since I am building with the Oxygen Builder in WordPress. I've tried setting the POST TYPES in CONTENT SELECTION to Photographs, Characters, and Characters Photographs Intermediary Posts then under TAXONOMY setting Photoshoots. I feel like it's the QUERY FILTER that will get me what I need but I can't figure it out.

Is there any documentation that you are following? No.

Is there a similar example that we can see?

What is the link to your site? hidden link

#2101733

Photograph has a Taxonomy called Photoshoot...There is a relationship between Photograph and Character...I am creating the Template for Character and would like to list all of the Photoshoots they were involved in.
Hello, this is unfortunately not so simple in Views, because it is not possible to filter a View of post type "A" based on the properties of related post type "B". If the Photoshoot taxonomy were applied directly to Character, you could create a View of the Photoshoot taxonomy and create a Query Filter based on the taxonomy term applied to the post where the View is displayed. Instead, the fact that the taxonomy is applied to a related post type makes this query less direct, and the solution involves several moving parts.

To work around the problem, you can create a nested View structure that generates related posts and terms. I will set up a test environment so I can walk you through the process, and explain these in a follow-up reply. I just wanted to let you know I'm working on this and it's a somewhat complex solution that will take some time. Please stand by.

#2101763

The solution requires 3 Views, which I will describe here. You should develop these in order, so that View 1 is functioning correctly first, then move on to View 2, then View 3. The process is a bit convoluted, as is the implementation, so it's best to give the Views names like View 1, View 2 and View 3 at least temporarily during development. My comments here will be easier to follow this way. You can rename the Views later without causing any problems, just be aware that if you change the View slugs later you need to update all the View shortcodes throughout the code to match the new slugs.

To get started, open the Character Content Template in one tab and open one of the Character posts on the front-end of the site in another tab. I suggest choosing a Character that has a relatively high number of related Photographs and Photoshoots. Then begin creating View 1 in a new tab so you can jump between tabs easily during development.

View 1
Create a new View in the legacy editor. In the first popup, choose the option "Display all the results" and name it View 1. In the main editor, choose (only) the Photograph post type in Content Selection. In the Query Filter section, add a Post Relationship or Repeatable Field Group Owner filter, choose the Photographs - Characters relationship, and choose the configuration ..."as related items of the post where this View is shown". Scroll down to the Loop Output section and click the Loop Wizard button. Choose the loop style "List with separators" then click "Add field", add the post title (for testing purposes), and click Finish. You will see the loop template generated automatically like this:

[wpv-layout-start]
  [wpv-items-found]
  <!-- wpv-loop-start -->
    <wpv-loop>
      [wpv-item index=other]
        [wpv-post-title],
      [wpv-item index=last]
        [wpv-post-title]
    </wpv-loop>
  <!-- wpv-loop-end -->
  [/wpv-items-found]
  [wpv-no-items-found]
    <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
  [/wpv-no-items-found]
[wpv-layout-end]

Delete everything inside the wpv-no-items-found shortcode, like so:

[wpv-no-items-found][/wpv-no-items-found]

Now place this View in the Content Template for Characters using a custom HTML block. Use a wpv-view shortcode with the attribute cached="off":

View 1:<br />
[wpv-view name="view-1" cached="off"]

Reload the Character post and now you should see a comma-separated list of the related Photograph post titles appear. The post titles are for testing purposes, we will remove them later. If the list shows the correct related Photographs, you're ready to move to the next View. I suggest a new tab for this.

View 2
Create another new View. In the first popup, choose the option "Display all the results" and name it View 2. In the main editor Content Selection area, choose the radio option "Taxonomy" and select "Photoshoot". Scroll down to the Query Filter panel, add a filter for taxonomy term, and choose the configuration ..."set by the current post". Scroll down to the Loop Output editor and run the Loop Wizard again, choose "List with separators" again, but this time insert the Taxonomy title. Finish, and you will see the loop template generated automatically again, like this:

[wpv-layout-start]
  [wpv-items-found]
  <!-- wpv-loop-start -->
    <wpv-loop>
      [wpv-item index=other]
        [wpv-taxonomy-title],
      [wpv-item index=last]
        [wpv-taxonomy-title]
    </wpv-loop>
  <!-- wpv-loop-end -->
  [/wpv-items-found]
  [wpv-no-items-found]
    <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
  [/wpv-no-items-found]
[wpv-layout-end]

Delete everything inside the wpv-no-items-found shortcode again. Instead of adding View 2 to the template, edit View 1 and replace the wpv-post-title shortcodes with a shortcode for View 2, caching turned off:

[wpv-layout-start]
  [wpv-items-found]
  <!-- wpv-loop-start -->
                 <wpv-loop>
                          [wpv-item index=other]
                                   [wpv-view name="view-2" cached="off"],
                          [wpv-item index=last]
                                   [wpv-view name="view-2" cached="off"]
                </wpv-loop>
  <!-- wpv-loop-end -->
  [/wpv-items-found]
  [wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]

Reload the Character post on the front-end of the site. You should see a comma-separated list of term titles. These should be the terms assigned to all Photograph posts related to the Character. You might notice that the list includes repeated terms, perhaps also some commas that seem unnecessary, and a somewhat random sort order. We need one more View to fix those issues. The best way to handle this is to use another View of terms filtered by term ID set by a shortcode attribute. This type of filter will help consolidate the terms from all the different Photographs in a way that is easily sorted, while ignoring the duplicates and extra commas. Those issues aside, if the list shows the terms you expect, you're ready to move on to the next View. I suggest a new tab. Create a clone of View 2 from Toolset > Views. Hover over the title, click the Duplicate link, and call it View 3 for now. Before you begin editing View 3, edit View 2 again and replace the term title shortcodes with term ID shortcodes:

[wpv-layout-start]
  [wpv-items-found]
  <!-- wpv-loop-start -->
    <wpv-loop>
      [wpv-item index=other]
        [wpv-taxonomy-id],
      [wpv-item index=last]
        [wpv-taxonomy-id]
    </wpv-loop>
  <!-- wpv-loop-end -->
  [/wpv-items-found]
  [wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]

View 3
Edit the new cloned View 3 and edit the existing term Query Filter. Change it to "Terms with ID set by the shortcode attribute: terms". Scroll down to the Loop Editor and click "Loop Wizard". This is the View where you ultimately set the design for the final output of the terms list, so choose the loop style you want and insert the fields you want to display for each term. If you choose the option to use a Content Template in the loop, you can use the Block Editor to design the loop template if you prefer.

Place View 3 directly in the Character Content Template using wpv-view with caching turned off. Add the "terms" shortcode attribute and insert some hard-coded Photoshoot term IDs for testing purposes, like this:

View 3 test with hardcoded ids:<br />
[wpv-view name="view-3" terms="123,456" cached="off"]

Replace 123,456 with commas separating the IDs of at least two known Photoshoot taxonomy terms. You can find term IDs in the URL of the page where you would normally edit the term in wp-admin. So go to Photograph > Photoshoot, click a term name, and look for the tag_id parameter in the URL. The value of tag_id is the term ID you need. Repeat that process to get at least two term IDs and place them in the View shortcode term attribute like shown in the previous code example. Reload the Character page and you should see the View 3 test text, followed by a term list displayed using the styles you added in the View so far. The design does not have to be perfect now, it's for a proof of concept.

Once that is showing up as expected, you're ready to combine all 3 Views. Add to the Content Template:

Nested Views:
[wpv-view name="view-3" terms="[wpv-view name='view-1' cached='off']" cached="off"],

You have nested the results of View 2 inside the loop of View 1, which is itself nested in the terms attribute of View 3. The result on the front-end should be a list of terms associated with the Photograph posts related to the Character currently being displayed.

Hope this helps, I will stand by for your updates and I can help if you get stuck or have questions about the setup. I will most likely need a login to jump into wp-admin if you need help, so I am activating private reply fields here.

#2101815

I should probably warn you, there is adult content. My site is an adult-based site. I can remove it since It is just now being added and there isn't more than a couple, but if you it doesn't bother you, there are only a couple adult images.

#2102393

I should probably warn you, there is adult content.
Thanks for the warning! It's no problem, I'm an adult

"set by the current post" is not an option...
Sorry it was not not clear, the first step is to select "Taxonomy term" in the popup. Then choose the configuration "set by the current post" back in the main editor screen, in the options for the term filter.

#2102583

I'm stuck... lol...
I've built all the views... but it's so far beyond me... I do not know your witchcraft... lol
I need to learn these nested views.. My brain is intrigued beyond measure

#2103087

Okay on the front-end, I see a list of 3 Photoshoots in the Character post here:
hidden link
- K.P. 2019
- F.S. 19JAN18
- F.S. 03SEP17

The C.U. Character post is related to these two Photograph posts:
hidden link
hidden link

The only Photoshoot term applied to those two Photograph posts is F.S. 19JAN18, so it seems that View #3 is not filtered as expected. I see View #3 placed in the Oxygen Character template using a Shortcode widget, here:
hidden link

But the shortcode is not quite right. The current shortcode is:

[wpv-view name="view-3" cached="off"]

...but the correct shortcode contains the terms shortcode argument and View 1 nested as the value of that argument:

[wpv-view name="view-3" terms="[wpv-view name='view-1' cached='off']" cached="off"]

Without that, no term filters are applied to the View of Photoshoots, and that results in all Photoshoots being displayed.
I tried making the necessary adjustment in the template, but ran into an issue. Oxygen's shortcode widget does not seem to support this nested shortcode structure. The shortcodes are not evaluated to display Views. Instead part of the shortcode is written out to the page with some numeric term IDs. I've seen this limitation in other 3rd-party page builders that offer shortcode widgets - Elementor, for example. I'm no Oxygen expert so I can't say for sure what you need here. I would experiment with another element type, like custom HTML, basic text, or WYSIWYG in text mode.

Let me know what you find out. If none of those elements work for nested shortcodes, we can try to come up with another solution for nesting these shortcodes and including the results in your Oxygen template.

#2103123

Can we create a 4th VIEW that has the necessary setup and just reference it? Meaning since:

[wpv-view name="view-3" terms="[wpv-view name='view-1' cached='off']" cached="off"]

The TERMS won't layout we create a view, put the terms in the output editor or Loop editor, wherever makes the most sense, and then just shortcode THAT whole view?

#2103127

Oh. I used the SHORTCODE WRAPPER and it appears to be doing something different.

#2103177

Oh. I used the SHORTCODE WRAPPER and it appears to be doing something different.
Now on the front-end I see the results I expect from View 1 (the term IDs), but not View 3. The outer View 3 shortcode seems to be ignored now, and it seems only the nested shortcode is executed:

[wpv-view name='view-1' cached='off']

So I'm not sure what Shortcode Wrapper does exactly, but it seems to disregard the outer shortcode for some reason and only display the results of the inner shortcode.

Can we create a 4th VIEW that has the necessary setup and just reference it?
Yes, this would probably work, but it could negatively impact performance and speed with yet another View query. I'm not sure if Toolset's Content Templates are supported in Oxygen templates, but if so, you could avoid the additional View and performance issue by placing the nested shortcodes in a Toolset Content Template, and inserting that Content Template in the Oxygen template using a wpv-post-body shortcode.

#2103505

SHORTCODE WRAPPER is so that you can wrap a shortcode around several objects like a DIV.
As for the "avoid the additional View and performance issue by placing the nested shortcodes in a Toolset Content Template, and inserting that Content Template in the Oxygen template using a wpv-post-body shortcode." yes please... guide me Obi Won... I'm waiting to learn.

After this is all over, I am hoping you can give me a rudimentary breakdown of what the views are doing. I've built databases (and databased apps) almost my entire adult life. So I snatched up Toolset the moment I saw it. But the Loop Editor in the View has ALWAYS been a mystery and the documentation has ALWAYS been so sketchy.

Please teach!

#2103845

Lets give it a try!

#2103937

The Content Template approach is pretty simple. You basically move the combined shortcode from the Oxygen template into a Toolset Content Template, and insert the Toolset Content Template in the Oxygen template instead, using a different shortcode.

So first, you would create a new Content Template in Toolset > Content Template, and do not assign it to any post type or archive. Paste the combined shortcode in the template.

[wpv-view name="view-3" terms="[wpv-view name='view-1' cached='off']" cached="off"]

If the Content Template is edited in the Block Editor, you can add a custom HTML block to the template and paste the combined shortcode in that block. If the Content Template is edited with the legacy editor, just paste the combined shortcode in the main editor area.

Then in the Oxygen template, delete the combined shortcode and replace it with a Toolset Content Template shortcode:

[wpv-post-body view_template="your-content-template-slug"]

Replace your-content-template-slug with the slug or title of the Toolset Content Template you just created.

That's the first approach I would try, but sometimes Content Templates do not cooperate in 3rd-party page builders. We'll have try it and see. If not we can try a different approach.

#2103969

Attempting now

#2103971

IT WORKED!!!!!!