Skip Navigation

[Resolved] Grouping posts in a view with an intermediary post type

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

Problem:

Two post types: "resource" and "resource list", with many-to-many relationship using below intermediate post type.

In a single "resource list" post, setup a post view:

- query posts of intermediate post type

- display related "resource" posts:

- order the results by numeric field "order"

- groups the results by taxonomy "Resource categories"

Solution:

You might consider to Move the numeric field "order" to post type "resource". See details here:

https://toolset.com/forums/topic/grouping-posts-in-a-view-with-an-intermediary-post-type/#post-1244948

Relevant Documentation:

This support ticket is created 5 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 2 voices.

Last updated by matthewC-9 5 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1243609

I'm working on two related post types: resource, and resource list. They have a many-to-many relationship, where each list includes a group of resources and a resource could be reused on multiple lists. A resource could have a different description on each list where it shows up, and I've got an intermediary post type that provides the description as well as a numerical field used to order the resources within the list. I've got a view based on the intermediary post type that shows each resource with a title and URL from the resource post type, plus the description from the intermediary post type, in the order set by the intermediary post type numerical field. So far, so good.

The part I haven't figured out yet is that some of the lists have subcategories within them - for instance, hidden link and hidden link. There are other lists (like hidden link) that don't have subcategories and shouldn't display any extra headings. I'm basically trying to figure out how to replicate this content using Toolset.

I started out trying to follow the instructions on https://toolset.com/2015/07/how-to-group-posts-by-category/, but ran into a snag because my view is based on the intermediary post type, which doesn't allow taxonomies or have taxonomy as a query filter option. What else could I try?

#1243909

Hello,

Please elaborate the question with more details:
How do you setup the post types and taxonomies?
Where do you want to display the view? in the single single "resource list" post or single "resource" post?

I need to test it in my localhost, thanks

#1244727
toolset-new-resource.PNG
toolset-new-list.PNG
toolset-connect-resource.PNG
toolset-resource-list.PNG

Hi! I would like to display the view on the resource list (called "Discover It Online list") post. See the attached toolset-resource-list.png screenshot for what I've got so far, without the category headings within the page. I want to get that page set up like hidden link.

I'm also attaching screenshots showing the new resource and Discover It Online list screens, plus the connection screen. Let me know what other details you need to look into this.

Thanks!

#1244948

I assume we are talking about the custom taxonomy "Resource categories" in your screenshot:
hidden link

And you are using below structures:
1) Two post types: "resource" and "resource list", with many-to-many relationship using below intermediate post type
2) there is a custom numeric field "order" in the intermediate post type
3) the custom taxonomy "Resource categories" is registered to post type "resource"

And you are going to do this:
In a single "resource list" post, setup a post view:
- query posts of intermediate post type
- display related "resource" posts:
- order the results by numeric field "order"
- groups the results by taxonomy "Resource categories"

If it is, it is expected result:
my view is based on the intermediary post type, which doesn't allow taxonomies or have taxonomy as a query filter option.

You might consider this:
1) Move the numeric field "order" to post type "resource"
2) in a single "resource list" post, setup a nested view:
a) Parent taxonomy view:
- query terms of taxonomy "Resource categories"
- in view's loop, display below child post view
b) Child post view:
- query "resource" posts
- filter by:
b1) post type relationship between "resource" and "resource list"
b2) Resource categories set by the parent Taxonomy View
- in view's loop, display "resource" post information

#1245215

Hi Luo - thanks for the reply, but if I understand right, I don't think this would quite accomplish what I'm trying to do. The reason I'm using an intermediate post type is that on different lists, the same resource could have 1) a different order and 2) a different description. Moving those fields to the resource post type would take away that ability, unless I'm missing something (which is very possible!). Any other ideas? I've thought about doing something like adding a new post type for "parent list" and pulling in multiple child lists instead of using a taxonomy, although I haven't figured out exactly how that would work either... I'm open to different ways of doing this and only set out to use a taxonomy because that's what that help article had suggested doing.

Thanks again--

#1245561

I don't think you are missing something, and you are correct, if you are querying posts of intermediary post type, you can filter/order by custom fields/taxonomies of intermediary post type, it is a limitation of WordPress WP_Query:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

I have already provide my suggestion above:
https://toolset.com/forums/topic/grouping-posts-in-a-view-with-an-intermediary-post-type/#post-1244948

in your case, you need to consider:
Move the numeric field "order" to post type "resource", so you will be able to order/filter the results by taxonoies/fields of post type "resource".

#1246127
toolset-list-2.PNG
toolset-list-1.PNG

OK, I think I'm getting it! So now I have the nested views set up to group the resources by category. A follow-up question: currently all of the taxonomy terms display on all of the list pages, even if there are no resources associated with that term for a given list - see attached. Is there a way to set a condition where the parent view only displays the taxonomy term if there are associated resources on that specific list? This is what I currently have in the loop editor for the parent taxonomy view:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
<h2>[wpv-taxonomy-title]</h2>
[wpv-view name="online-resource-take-2"]
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

#1246227

You can try to move the taxonomy title into child post view "online-resource-take-2", for example, edit the codes you mentioned above, from:

...
<h2>[wpv-taxonomy-title]</h2>
[wpv-view name="online-resource-take-2"]
...

To:

...
[wpv-view name="online-resource-take-2" term_title="[wpv-taxonomy-title]"]
...

Edit the child post view "online-resource-take-2", in section "Loop Editor", display term's title, like this:

...
[wpv-items-found]
[wpv-attribute name="term_title"]
....

[/wpv-items-found]
...

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-attribute
This shortcode displays the value of an attribute in a View shortcode.

#1246661

OK, great - that worked! One more follow-up question: for some lists, there won't be any subcategories (ie none of the taxonomy terms used), and for those lists, I would ideally like to fall back to a different view instead. Right now I've got:

[wpv-layout-start]
[wpv-items-found]
<h2>[wpv-attribute name="term_title"]</h2>
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-post-body view_template="loop-item-in-online-resource-take-2"]
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpv-view name="online-resource-description-for-list"]
[/wpv-no-items-found]
[wpv-layout-end]

This is close, except that the online-resource-description-for-list view repeats once for each taxonomy term from the parent view. Is there a different way to accomplish this without the repetition?

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/one-more-follow-up-question/

#1246853

For the new question, please check the new thread here:
https://toolset.com/forums/topic/one-more-follow-up-question/

#1254589

My issue is resolved now. Thank you!