Skip Navigation

[Resolved] nested posts relationships views

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

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

Assisted by: Christian Cox.

Author
Posts
#625996
relationships.JPG

Tell us what you are trying to do? We need to achieve the follwing posts relationship views:

We have a PROJECT post
The Project Post shall view in the sidebar a list of COMPANIES in a UL list
Within each LI of that list we need to show the ARCHITECTS that only worked for that specific PROJECT and they are already connected in a many-to many relationship with both COMPANIES and PROJECTS

I'll show you an example in the screenshot below:

We need to display only the Architects that worked for that PROJECT (they are only some of the architects belonging to the 2 companies)

#626019

This type of display looks deceptively simple. To accomplish it in Views takes several steps.

Step 1: We need to create a list IDs of all the Companies related to this project. Something like 1,2,3,4.
- Create a View of Architects filtered by post relationship, where the parent is the current page (i.e. the Project)
- In the Loop, insert the post ID of the parent Company using the wpv-post-id shortcode like this:

<wpv-loop>
      [wpv-item index=1][wpv-post-id id="$company"]
      [wpv-item index=other],[wpv-post-id id="$company"]
</wpv-loop>

- Replace $company with "$" plus the company post type slug, if it's not "company".
- This should produce a list of Company IDs, but it will include some extra markup we do not want. So add this filter to your child theme's functions.php file:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
  $ids = array( 12345 );
  if ( in_array( $id, $ids )) {
    $start = strpos( $out, '<!-- wpv-loop-start -->' );
    if (
      $start !== false
      && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
    ) {
      $start = $start + strlen( '<!-- wpv-loop-start -->' );
      $out = substr( $out , $start );
      $end = strrpos( $out, '<!-- wpv-loop-end -->' );
      $out = substr( $out, 0, $end );
    } else {
      $start = strpos( $out, '>' );
      if ( $start !== false) {
        $out = substr( $out, $start + 1 );
        $end = strpos( $out, '<' );
        $out = trim(substr( $out, 0, $end ));
      }
    }
  }
  return $out;
}

- Replace 12345 with the numeric ID of the View of Architects.
- Place this View somewhere on the Project page, and check that it outputs the correct Company IDs. It's okay if there are duplicates.

Let me know if you are able to get this far, and then I'll go into the next step.

#626035

Hi Christian thanks for your help.
I will follow your instructions tomorrow at work. In the meantime if you could write me the other steps to complete the task I will look at them and work until the final solution.

Best,

Franco

#626123

Hi Christian,
I followed your instructions but I don't know if I did all OK because something doesn't work. In the layout screen I find the list of architects displayed in the view but in the post frontend there are no results displayed (see screenshot).
What could be the problem?
I already succeeded before your tutorial in displaying the companies related with that project but it displays all architect of all companies instead of filtering the architects that only belong to that project.
you can have a look at the page at the following link:
hidden link

How can we proceed to solve the issue in your opinion?

Maybe we made some mistakes in the posts relationships...

Franco

#626249

I followed your instructions but I don't know if I did all OK because something doesn't work. In the layout screen I find the list of architects displayed in the view but in the post frontend there are no results displayed (see screenshot).
The instructions were to create a list of Company IDs. The point of that exercise is to create a list of IDs we can use as a Views filter, so we can create a list of Companies related to this Project. Is that how you created your View of Companies, or did you take another approach? The approach I described is best because it prevents duplicate Companies from appearing in the results. Otherwise you can end up with duplicate Companies in the list when more than one Architect in the same Company works on the same Project.

The next steps in summary:
- Create a View of Companies, filtered by Company ID, supplied by a shortcode attribute
- Insert the View of Companies in the current Project and nest the View of Architects in its post ID filter shortcode attribute, to produce a list of Companies in the current Project. This is called passing arguments into a View: https://toolset.com/documentation/user-guides/passing-arguments-to-views/
- Create a second View of Architects, this time with no filters, and check the box "Don't include the current page in the results".
- Insert the post title in the Loop Output of the second View of Architects.
- Add custom code that applies a query filter to the View of Architects using two post relationship filters, in functions.php:

add_filter( 'wpv_filter_query', 'filter_both_parent_types', 10, 3 );
function filter_both_parent_types ( $query, $view_settings, $view_id ) {
  global $post;
  $views = array( 12345 );
  if( in_array( $view_id, $views) ) {

    $company_id = $post->ID;
    $args = array(
      'relation' => 'AND',
      array(
        'key' => '_wpcf_belongs_company_id',
        'value' => $company_id,
        'compare' => '=',
        'type' => 'string'
      ),
      array(
        'key' => '_wpcf_belongs_project_id',
        'value' => $query['post__not_in'][0],
        'compare' => '=',
        'type' => 'string'
      )
    );

    // add these arguments to your meta query
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }
 return $query;
}

- Insert this View of Architects in the View of Companies loop.

#626254

Hi Christian,
in the structure I built (before your help) I created some many-to-many relationships between the following post types:

PROJECT-COMPANY
COMPANY-ARCHITECT
ARCHITECT-PROJECT

because each one of them can have a multiple relationships, so no parent/child relationship have built between those post types.

Please let me know if this structure of many-to-many relationship is OK in order to build your tutorial, because I tried today to follow your steps but there is somethin maybe I did wrong... I have a knowledge of Toolset since I have created another project before but in some steps of your tutorial I did not understand how to do the things you explained.

So what do you think it is the best approach to solve this topic?

Franco

#626274

Okay I see, I misunderstood your description of the post types and relationships. I thought that Architect is an intermediary post type connecting Projects and Companies in a M2M relationship. That is not the case here, you actually have 3 separate M2M relationships. If Architect John works for Company A on Project 1, for Company B on Project 2, and for Company C on Project 3, this View becomes difficult to achieve.

On the Project 1 page, how do you know that John worked for Company A on this Project? You know that John worked on the Project because of the M2M relationship between Project and Architect. But you have no way of knowing which Company he was working for at the time because his relationship to the Company is separate from his relationship to the Project. So I think you have a situation that doesn't really work with 3 M2M relationships between these post types.

I think one option to solve that is to create a new intermediary post type that is a child of all 3 other post types. This will let you create a single association between all 3 post types. In the steps I outlined above, Views of Architects will be changed to Views of intermediary posts.

#626276

OK Christian,
let me see if I understood:
I have to create a new post type (let's call it WORK) and it will be child of all 3 post types (ARCHITECTS, COMPANY AND PROJECT).
Then in the post types of ARCHITECTS, COMPANY and PROJECT shall I also setup the association between the 3 post types in addition to the association of the M2M relationships that are already active or the new M2M relationship will override the other M2M that can be deleted?

#626282

Hi Christian,
I created the new relationship post and I tried to create a view that shows all relationships for that project concerning companies and architects.
The result is correct
hidden link
but it shows multiple times the company link. Is there a way to have only one studio link with the architect list under that?

Franco

#626308

I have to create a new post type (let's call it WORK) and it will be child of all 3 post types (ARCHITECTS, COMPANY AND PROJECT).
Yes, correct.

Then in the post types of ARCHITECTS, COMPANY and PROJECT shall I also setup the association between the 3 post types in addition to the association of the M2M relationships that are already active or the new M2M relationship will override the other M2M that can be deleted?
If you do not need the other M2M relationships for any reason, delete them. The new M2M relationship you will create will be used for the purpose of this View.

but it shows multiple times the company link.
That's right, because you probably did not first create a View that returns a list of Company IDs as recommended. If you do not follow this step first, and apply it to a shortcode attribute, you will get duplicate results. Please refer to my previous comment about that here:
https://toolset.com/forums/topic/nested-posts-relationships-views/#post-626249

#626699

Hi Christian,
I followed your instructions but I get some strange results.
You can have a look at the following page where in the first view result you see the list of IDs of the companies.

hidden link

In the second list where you see a set of 6 no-items-found elements I inserted the view generated by your full tutorial.

How can I show you the various views and parameters I used in a simple way? I thought I followed your instructions but it is not, so i need to find out where i did the mistake.

Please let me know how is the best way to debug this.

Thanks in advance for your help.

Franco

#626700

Hi Christian, I requested a Google Hangout call with you so it will be a good method to solve the issue.
Would you be available for that?
Franco

#626733

My manager is responsible for scheduling those, and it often takes a while to get together. If you'd like to provide login credentials for your site, I can take a look at how you have things set up and make some recommendations. I will activate private reply fields here so you can share login credentials in confidence. Let me know where I can see the results you are describing.

#627027

Okay please check now, I made some adjustments and created some new Views so I would not disturb anything else on the site: hidden link

#627054

Fantastic work Christian, you're the best 🙂

I will look at the views to see what I did wrong... (do you have any idea where I did the mistakes?).

The only thing I have to change is that I need both the STUDIO and the ACRHITECT names to be linkable to their posts.
How can it be done?

Franco