Skip Navigation

[Resolved] View filtered by two taxonomies and loop sorted by one taxonomy

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

Author
Posts
#1669947

Hi!

I've been racking my brain for hours, but I can't figure it out.

I’d like to create a view which lists posts of one post type sorted by two taxonomies. The filtering/sorting should be nested.

Example Setup:
Custom post type “Training”
Custom taxonomies “Category” and “Type of presentation”

The view then should do this:

Show all posts of a specific “Category” term (definded by taxonomy filter), e.g. “Service” or “Sales”.
Then loop through all “Type of presentation” terms and list the posts sorted be the “Type of presentation” terms.

View “Service”:

All posts of category “Service”

Type of presentation “e-Training”
Post 1
Post 2
Post 3

Type of presentation “Webinar”
Post 4
Post 5
Post 6

Type of presentation “Podcast”
Post 7
Post 8
Post 9

Output:

<h1>Service</h1>

<h2>e-Training</h2>
<ul>
	<li>Post 1</li>
	<li>Post 2</li>
	<li>Post 3</li>
<ul>

<h2>Webinar</h2>
<ul>
	<li>Post 4</li>
	<li>Post 5</li>
	<li>Post 6</li>
<ul>

<h2>Podcast</h2>
<ul>
	<li>Post 7</li>
	<li>Post 8</li>
	<li>Post 9</li>
<ul>

Another view “Sales”:

All posts of category “Sales”

Type of presentation “e-Training”
Post 10
Post 11
Post 12

Type of presentation“Webinar”
Post 13
Post 14
Post 15

Type of presentation “Podcast”
Post 16
Post 17
Post 18

Output:

<h1>Sales</h1>

<h2>e-Training</h2>
<ul>
	<li>Post 10</li>
	<li>Post 11</li>
	<li>Post 12</li>
<ul>

<h2>Webinar</h2>
<ul>
	<li>Post 13</li>
	<li>Post 14</li>
	<li>Post 15</li>
<ul>

<h2>Podcast</h2>
<ul>
	<li>Post 16</li>
	<li>Post 17</li>
	<li>Post 18</li>
<ul>

Finally, it is possible, that one post is on more than one category and/or type of presentation. For example, "Post 20" is in the categories "Service" and "Sales" and in the presentation types "Webinar" and "Podcasts".

Is it possible to create views for this? If this is unclear, I can also create a scribble.

Best regards
Matthias

#1671455

Hi! There's nothing exactly like this built-in to Views, unfortunately, but I can give you some suggestions to help you get close to this. First, a few questions:
- Do you expect to have posts that match every term combination, or could there be a combination of terms that produce no results?
- Do you expect to have enough results to require some kind of pagination, or will there be just a few total posts?
- What should be displayed before the User selects a filter?

Side note:
Custom taxonomies “Category” and “Type of presentation”
I'm not sure if you meant you want to use the built-in WP taxonomy "Category" or if you are expecting to create a custom taxonomy called "Category." The word "Category" is a reserved term in WordPress and you should not attempt to create a custom taxonomy called "Category". Not only will it be confusing, it can cause redirection issues and 404s on the front-end of the site. Either use the built-in Category taxonomy with this custom post type (you can do this in the post type editor screen) or come up with a different name for your custom taxonomy.

#1671827

Hi Christian,

Thank you for your reply.

1. In theory, this is possible. But in practice, there will be only one combination for every post. Maybe a few posts are assigned to more than one “type of presentation” term.
2. No pagination required. There will be only a few posts for every taxonomy term.
3. No results: Yes, this could happen if some older posts are deleted and the terms still exist. But then these terms shouldn’t be displayed in the frontend.
4. There won’t be a filter. I simply will create 4 default pages for every “category” term and will setup a view for every page. There will be only 4 “category” terms at the moment.

Side note:
I know the problem. Probably, I will give the “category” taxonomy another title/slug like “Topic”.

Best regards
Matthias

#1672649

Okay so yes, you can do this with two Views:
1. A View of posts filtered by both taxonomies, set by two shortcode attributes like wpvyourtax1 and wpvyourtax2.
2. A View of Type of Presentation terms.

Use the wpv-attribute shortcode to pass shortcode arguments between Views. Place the View of Type of Presentation terms in each Page you have created for the different Category terms. Pass the current Category term slug into the Type of Presentation View using a shortcode argument. Then pass that slug, the current Type of Presentation term slug, and the current Type of Presentation term name into the View of Posts. Then place the View of posts in the loop of the Type of Presentation terms. In addition to the posts, display the Type of Presentation term name in the View of posts in the wpv-items-found section, so the term is only shown if there are posts found.

If you're not familiar with shortcode attributes, check out this documentation: https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/

#1673585

Hi Christian,

thank you for your reply.

I understand the idea of nesting the views but I don't understand how to pass the arguments between the views. I tried a lot of settings but I can't get the proper output.
I have setup a test installation with some posts and terms and 2 views. Maybe you can take a look at my settings?

Best regards
Matthias

#1675057

Yes of course, I'll take a look and give you some feedback. Please provide login credentials here in the private reply fields and let me know where I can find the test setup on the front-end of the site (a URL where I can see the Views). Thanks!

#1675123

I'm sorry but where is the private reply field?

#1676557

You should find the private fields attached to the reply form when you respond to this message.

#1677115

Okay please check now. Each topic page has a View shortcode with the corresponding topic slug, like this:

[wpv-view name="2-view-of-terms" wpvtopic="sales"]

The View of terms (outer View) has a loop that looks like this, containing the nested View of posts with three shortcode attributes, like this:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<ul class="wpv-loop js-wpv-loop">
		<wpv-loop>
			[wpv-view name="1-view-of-posts" wpvtopic="[wpv-attribute name='wpvtopic']" wpvtypeofpresentationslug="[wpv-taxonomy-slug]" wpvtypeofpresentationname="[wpv-taxonomy-title]" ]
		</wpv-loop>
	</ul>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]

The View of posts (inner View) actually contains the term name to prevent empty terms from displaying in the term list. The View has a loop that looks like this:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start --> 
  <li>
[wpv-attribute name="wpvtypeofpresentationname"]
	<ul class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<li>
				[wpv-post-link]
			</li>
		</wpv-loop>
	</ul>
  </li>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]
#1677719

Hi Christian!

This is what I was looking for! Thank you very much for your help!

Best regards
Matthias