Skip Navigation

[Resolved] Creating a View filtered by multiple taxonomies

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

Problem: I have created a custom rewrite rule applied to custom taxonomy archives, such that the archive URL includes term slugs from two different taxonomies. I would like to filter a View placed in this archive by these two taxonomy term slugs.

Solution: Create two Query Filters in the View, one for each taxonomy term slug. Set each to respond to a different shortcode attribute. Use custom shortcodes to apply the term slugs from the URL to the corresponding shortcode attributes. Test each component individually by applying hard-coded shortcode attributes in the View shortcode and outputting the results from the custom shortcode separately.

This support ticket is created 3 years, 3 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 10 replies, has 2 voices.

Last updated by stevenM-7 3 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#2134691

Tell us what you are trying to do?
I have a custom post type (articles) and I would like to create a listing of posts filtered by both topic (a custom taxonomy) and tag. I have created a custom rewrite where this works. The URL is /articles/{topic_name}/{tag}. This works properly if I have no WP or elementor Archive page templates set up. However, if is try to use either archive page (WP or elementor) the view is not filtering by Topic, only Tag): hidden link - There should only be one post on this page, the first one. The second post (The Auditors) is not in the Politics Topic.
Is there any documentation that you are following?
I have been googling this and found a few posts within the forum but nothing to clearly indicate why this isn't working.
Is there a similar example that we can see?
The View I built to display and filter the articles is here: hidden link. I built a WP article page using this view and have this being used on Tag and Category pages: hidden link (Topics and Categories are the same...the client named the Custom Taxonomy Category but the slug is Topic)
What is the link to your site?
hidden link

#2135129
Screen Shot 2021-08-05 at 4.57.35 PM.png

Hello, if I understand correctly you are saying that the post "On Human Shields" contains the term "war" in the Tags taxonomy, and also contains the term "war" in the Topic taxonomy, so you expect it to appear in the archive at hidden link, is that correct? In other words, you want to filter the View by term in both taxonomies. The results must contain the current archive term in both taxonomies.

If that is correct, then I would adjust the Query Filters in this View a bit. I would change them so that the filter for term slug is set by a shortcode attribute like "term", instead of filtering by terms set by the current archive page. See the screenshot here for a Query Filter example showing the preferred setup.

Then, I would place the View using a shortcode, and pass the current archive term slug into the "term" shortcode attribute using the wpv-taxonomy-archive shortcode. This will filter by term in both taxonomies, based on the current archive term slug:

[wpv-view name="your-view-slug" term="[wpv-taxonomy-archive info='slug']"]

If you're using the Block Editor, I suggest placing this shortcode in a custom HTML block, not a Shortcode block. Shortcode blocks often have trouble with nested shortcode structures like this, so custom HTML is usually better. Let me know if I've misunderstood what you want to achieve here.

#2135603

Hi, What you explained is a bit different - that the post "On Human Shields" contains the term "war" in the Tags taxonomy, and also contains the term "Politics" in the Topic taxonomy, so you expect it to appear in the archive at hidden link.

However, I think your solution would still work. When I tried to implement it however nothing is displaying on the page: hidden link

I have the view set up as in your screenshot: hidden link and then I put the shortcode you gave me on the archive page I had set up: hidden link. Is there something I am missing or did incorrectly?

#2137005

What you explained is a bit different - that the post "On Human Shields" contains the term "war" in the Tags taxonomy, and also contains the term "Politics" in the Topic taxonomy, so you expect it to appear in the archive at hidden link;
I assumed the term Politics is the parent of the term War in both taxonomies. The term Politics is not directly applied to the On Human Shields post, not in either taxonomy. The term War exists in the Tags taxonomy and also in the Topics taxonomy. The term War from the Tags taxonomy is applied to the On Human Shields post, and also the term War from the Topics taxonomy is applied to the On Human Shields post. If that was not your intention, I misunderstood what you want to accomplish. Can you clarify to help me understand what you expect to see in the results of this archive View, based on the example URL?
1. Does the structure /politics/war/ imply a hierarchical term relationship where Politics is the parent term and War is the child term? Or are the terms Politics and War unrelated?
2. Is the order of the terms important? In other words, do you expect to always filter based on the first term (Politics) in the Tags taxonomy, and always filter based on the second term (War) in the Topics taxonomy? What if a different post has the term Politics in the Topics taxonomy and the term War in the Tags taxonomy, would you expect to see that post in the results when the terms are in opposite taxonomies?
3. Should the filters require an "AND" combination of terms, or an "OR" combination of terms? In other words, are both matching terms required for a result (A and B), or is only one matching term required for a result (A or B)?

#2138341

Tags and Topics do not have any sort of relationship to each other, they are separate taxonomies, neither one is a parent of the other. I do not understand what you say when you mentioned that Politics is not directly applied to the "On Human shields" post. This is the article and it has "Politics" as a Topic and "war" as a term: hidden link. Again, please remember that Topics and Categories in this case are the same because of how the client set this up: hidden link.

To answer your other questions:
1. The URL structure /politics/war is a custom rewrite to pull only articles within the category/topic of politics and with the term of war. It is a custom rewrite I added to the functions.php file. It is not hierarchical in the sense that the tag "war" can only be found in the topic of "politics", "war" can be tagged to other posts in other topics, they just wouldn't show in this list.
2. The order does not matter, this results listing should show any and all posts that have the topic of "Poltics" and the tag of "war". In your example, if another post was in the "Politics" topic and was tagged with "war", yes it should show in this results listing.
3. Yes, this is a AND filter, both terms are required.

#2139677

Oh, facepalm. I had forgotten your original comment explained this custom rewrite rule. I'm sorry for the confusion and making you repeat that. I'm afraid the built-in Views taxonomy Query Filter options based on the current post or the current archive won't work correctly in this case, since you're manipulating the query programmatically. I think you'll need a bit of custom code here to return each term slug so you can pass those into the View's Query Filters using shortcode attributes. In other words you need one shortcode that returns the Topic term slug "politics", and another shortcode that returns the tag slug "war" - or one shortcode that can return either slug based on a variable shortcode attribute. Then you would need to set up two Query Filters in your View:
- One Query Filter for the Topic taxonomy, configured as a taxonomy term filter with slug set by one shortcode attribute like "topicslug".
- One Query Filter for the Tag taxonomy, configured as a taxonomy term filter with slug set by one shortcode attribute like "tagslug".

Then you would register the name of your custom shortcode(s) in Toolset > Settings > Front-end Content > Third party shortcode attributes. Once registered, you can place your custom shortcode(s) in the appropriate View shortcode attribute like so:

[wpv-view name="your-view-slug" topicslug="[custom-shortcode-topic][/custom-shortcode-topic]" tagslug="[custom-shortcode-tag][/custom-shortcode-tag]"]
#2140355

I have been trying to follow your above directions but I'm getting stuck at the custom short codes. I do not understand what I'm supposed to be adding for these.

I updated the View to use the "topicslug" and the "tagslug" like you suggested: hidden link But I do not understand what to put in the html shortcode within my archive page: hidden link I have tried quite a few things and I have these added here: hidden link But it's still not filtering by the topic, only by the Tag: hidden link

#2140365

I have been following you direction above but I am getting stuck at the custom short code. I do not understand what to do for this.

I have updated the view to add the "topicslug" and the "tagslug" as you suggested: hidden link and I have the short code in the HTML on my archive page (which is set to display for Tag and Category archive pages), however I do not understand what to add here: hidden link I have added these in the settings: hidden link But I don't think this is right because on the front end this is still only filtering the posts by tag, not by topic: hidden link

#2140429

I have tried to reply to this twice, so I apologize if you have received multiple replies....

I have tried to follow your instruction above but I am getting stuck on the custom shortcode, I do not understand what to do/how to do this.

I updated the view with the "topicslug" and the "tagslug" as you suggested: hidden link I then updated my archive page (which is configured for Tag archive and Category archive pages) to have the new custom HTML shortcode. However I am unclear on what to add in these areas: hidden link I don't understand what the custom shortcode is supposed to be. I have these in the settings: hidden link but I don't think this is correct because on the front end I still only have the listing being filtered by Tag and not Topic and Tag: hidden link

#2140441

I have updated the view to add the "topicslug" and the "tagslug" as you suggested: hidden link;
Okay this looks good.

I have the short code in the HTML on my archive page (which is set to display for Tag and Category archive pages), however I do not understand what to add here: hidden link
For a better understanding of what you need to accomplish, look at this shortcode. This is what you need:

[wpv-view name="posts-listing-categories-tags" tagslug="war" topicslug="politics"]

My expectation is that hard-coding the slugs like this will produce properly-filtered results in the View. To make this dynamic so that it works on all the article archive pages, you need to replace war and politics with shortcodes that produce the corresponding term slugs. To confirm the shortcodes are working as expected, place the inner shortcodes somewhere outside of the View shortcode to debug the results of standalone shortcodes. This is example test code:

tag slug shortcode: [wpv-taxonomy-archive info='slug']<br />
topic slug shortcode: [custom-shortcode-topic info='slug']<br />
view with hard-coded shortcode attributes: <br />
[wpv-view name="posts-listing-categories-tags" tagslug="war" topicslug="politics"]

On the front-end of the site, the expectation is that you will see:

tag slug shortcode: war
topic slug shortcode: politics
view with hard-coded shortcode attributes: 
(( you should see the properly filtered view results here ))

1. I'm not sure if the wpv-taxonomy-archive shortcode will work correctly in this context because of how you are altering the query in this archive.

2. I'm assuming you have created and coded in PHP a custom shortcode called custom-shortcode-topic in functions.php, or in a custom snippet somewhere. If you created it and the results seen for the test code are not correct, please share the PHP code you wrote so we can review. If not, you must create this custom shortcode using the WordPress Shortcode API. Docs and examples here:
https://codex.wordpress.org/Shortcode_API

Let's try the test code above and see what needs to be changed. Please copy + paste the test code into your site, replacing the View shortcode you currently have in the archive. Then copy + paste the results here for me to review.

I have added these in the settings: hidden link;
The custom shortcode registration looks correct here, but it is not necessary to register wpv-taxonomy-archive. Native Views shortcodes need not be registered here, registration is only required for 3rd-party (non-Toolset) shortcodes. I would delete wpv-taxonomy-archive here.

But I don't think this is right because on the front end this is still only filtering the posts by tag, not by topic:
Most likely the custom-shortcode-topic shortcode is not producing the proper results because of a bug or because it hasn't been created yet. The test code above should tell us what we need to change. If the custom shortcode is producing the proper results, something is wrong with the query for the View. Maybe I've overlooked something, maybe the archive query is interfering somehow, maybe something else I can't predict is happening. I'll need to check the View debug information for this page to get a better idea.

#2141277

It was the custom php that was causing the issue. I was able to resolve this and your debugging was extrememely helpful. Thank you very much to assisting me and working through this!