Skip Navigation

[Resolved] Add two views together

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

Last updated by Christian Cox 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#911474

Hi there

Tell us what you are trying to do? I would like to add two views seamlessly together.

Is there any documentation that you are following? No

Is there a similar example that we can see? No

What is the link to your site? Here you can se our current setup:hidden link (in the tabs below in the tab "Mitglieder"). The two upper persons belong to one view, the person below to another view with the same settings.

I would like to have the third person on the first line as well. Is this possible with two separate views or can I combine the two views in a third view where I have a loop that adds another loop with a different view as soon as the first loop has ended?

If this is possible, that would acutally solve many challenges we currently face on this website.

Thanks a lot for your help!

Best,
Marcial

#911588

Hi, it's not easy to do this unless the number of results in each View never changes. It may be more practical to create a single View that includes all the results, filtered and sorted like you see here. Can you tell me more about why you currently need to use 2 Views? What are the Query Filters for each View? What order or sorting should be used? I might be able to make a recommendation, or offer some custom sorting help.

#911936

Hi Christian

We need two relationships in two post types, that's the issue.

Currently we use the two views because we have two different relationships that should be displayed:

Person --> Team Section
Manager --> Team Section

If we could use the "all relationship" filter and then say not the others, that would be amazing.

So the views do the following:

View 1: call all person who have the relationship team section
view 2: call all manager who have the relationship team section

Do you have another suggestion, how we could solve this?

Best, Marcial

#912060

I'm not quite clear about the relationships. Are these one-to-many relationships, or many-to-many relationships? If they are one-to-many, which post type is the parent and which post type is the child?

Can you share a screenshot of the Query Filters section in the View editor screen?

#912244
persons.JPG
managers.JPG

Hi Christian

They are many-to-many.

Just a quick question in between: I think we could easily fix the problem by switching the "manager" to the same post type as the other "persons" and chose a different layout for those that are "managers". Is it possible to change the URL of a "manager" to something different than what's the default for this post type?

The reason why I ask is because the problem is caused by the two post types. We need for the "managers" to check wheter they are "members" of this team-section (and display them if they are) AND check the same for the "person". The problem is that this are two different relationships (one for each CPT). But we can't use two relationships in one query filter. (--> is there a way to have only one relationship for both, like from many-to-one = many managers and many persons to one specific team-section?)

Therefore I was thinking about adding those two views together. Currently it separates them on a new line as you can see here: hidden link

Maybe you remember helping me in another ticket (https://toolset.com/forums/topic/order-posts-in-one-view-from-two-categories-and-alphabetically/) where we discussed something similar. The code there didn't worked (something else broke when enabling this, but I hadn't the time to investigate it yet) but maybe the same attempt would help us this time as well?

Thanks a lot for your help!

Best,
Marcial

#912593

I have an example using the Toolset and Views APIs that will allow you to show both post types in one View, while selecting only specific post relationships. First, remove the post relationship filter from this View. Then add code similar to this in functions.php:

add_filter( 'wpv_filter_query', 'query_specific_relationships',99,3 );
function query_specific_relationships( $query_args, $views_settings, $view_id) {
  $view_ids = array( 12345 );
  $related_ids = array( );
  if (in_array($view_id, $view_ids)){
    $rels = array('room-library', 'book-library'); // only query these relationships
    $lib = get_post();
    foreach( $rels as $rel ) {
      $thisRel = toolset_get_related_posts(
        $lib,
        $rel,
        'child',
        9999999,
        0,
        array(),
        'post_id',
        'parent'
      );
      if ($thisRel) {
        $related_ids[] = $thisRel[0];
      }
    }
  }
  $query_args['post__in'] = $related_ids;
  return $query_args;
}

Replace 12345 with the numeric ID of this View. Replace 'room-library' and 'book-library' with the slugs of the two M2M relationships. This code assumes that the Team Section post type is set as the "child" in both M2M relationships. If that is not the case, I can show you some modifications you can make to fix that. I need to know which post type is the parent and which is the child in each M2M relationship. If you're not sure which is the parent and which is the child, edit the post relationship and check "I understand that changes to these settings may delete post associations in this Relationship", then click "Edit Settings". The parent and child will be defined here.

The code also assumes that 9999999 is the maximum number of related posts. The current API requires a positive integer here, so I selected a large number. There is a feature request to allow unlimited queries, but it's not implemented yet.

This code overrides any selections you make in a post ID filter in the View editor screen.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.