Skip Navigation

[Resolved] Relationships not showing data from a content template

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

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

Assisted by: Christian Cox.

Author
Posts
#1094148

Sorry, maybe I misunderstand. Basically, I want to show staff members' data (a photo and a post link) that are in the department *that the current staff member is on*. (the one you are viewing)

For example, I am on the Online Department. It would show me, and the other people in it.

Does that make sense?

#1095013

I'll quote your recent statements:
I can't call that view from the staff members single prototype page located here:...
...For example, I am on the Online Department. It would show me, and the other people in it.
I'm confused because of the difference between these statements. The first statement implies you want to show the View on the Staff Member single post. The second statement implies you want to display the View on the Online Department single post. While the difference may seem insignificant, it is quite important to the context of the View and the filter. On the Online Department single post, the sibling Staff Members can be determined by their relationship to the Online Department. On the Single Staff member single post, the sibling Staff Members are more ambiguous because there is no reference Department.

Does this make sense?

#1095024

I want to display this view on the single staff member page template. This view should have the context of the staff member's current department (as in the sub-department they are related to.) and be able to then display other staff member fields, in addition to the current one. so me + the staff members in my department.

#1095151

Let me ask you this way: Are any of your fellow Online Department Staff Members also part of another Department? I assume so because you set up a M2M relationship between Staff Members and Departments. Let's say "Mary Smith" is in 3 different Departments, including the Online Department.

Consider a site visitor searching for Mary by typing her name into a search form, and then clicking a link to visit Mary's single post page. In this Visitor's mind, there is no "current Department", they only know they are visiting Mary's Staff Member profile page. So what siblings should be displayed for Mary on this page? The siblings in the Online Department? The siblings in another Department you are not a part of? All siblings in all Departments that Mary is a part of? This is what I'm trying to understand. On the Department post, obviously there is a Department context. The siblings are all part of the Department. But on the Staff Member post, there is no Department context. There is no single "current department". There can be multiple related Departments and none of them are more or less current than the others.

#1095608

The siblings in the online department. Thank you for your thoroughness and patience. The people in multiple departments is a rare occurrence.

#1096163

Great, now we're getting close. What prompted you to make that decision, and why? I need to know to be able to set the View up correctly, and your answer didn't explain why you chose Online Department over the two other Departments Mary belongs to. My guess is you chose Online Department because you and Mary are both members, but your site Visitors may not be Staff Members and may not share any Departments with Mary so I need some other criteria.

This decision criteria must be clear and unambiguous, like "if there is more than one department, choose the first one in alphabetical order of Department post title" or "if there is more than one department, choose the one published most recently" or "if there is more than one department, choose the one with the most staff members" or "if there is more than one department, sort the departments using a custom field applied to the Department post, and choose the first one in order" or even "it really doesn't matter, there is no reason to choose one or the other Department instead of another, just make it choose something".

#1096290

Here's what I am thinking. Also, you are awesome. I think that the case of Mary is so rare within the org, and actually I may have constructed the relational structure differently today, that I'd like to operate the view for people that are in one department. If they are in more than one, the easiest thing to do would be to not show it or output a friendly message like, "This person is so involved that they are on more than one department/team."

#1096294

I just talked with my designer. What we would like to do for parent departments and departments is to show each of them that the person is connected to. The great thing about teamwork is that wasn't as verbose as my answer, and makes much more sense.

We also need to consider a limit so that it scales -- because that's the responsible thing to do, and helps us with another use case later.

#1096365

Okay so you want to create a list of Departments that are related to the current Staff Member in the M2M relationship. You will display that list on the Staff Member single post. That's pretty straightforward, and you'll do it like this:
- Create a View of Department posts, filtered by your M2M relationship, and configure the Query Filter to select posts that are related to the post where this View is shown.
- In the Loop Output editor, insert a shortcode to display a link to the Department post, or a simple post title shortcode if you don't want links.
- Insert the View of Department posts in the Single Staff Member Content Template or Template Layout.

Now the View will be shown on every Staff Member post, and will automatically show all the Departments that are related to the current Staff Member.

#1096372

What I want to do is the following:

Display staff members related to any department related to that staff member.

Hopefully that's clear.

#1096391

Okay yes, that makes sense. This type of filter isn't built in to Views, but I can show you how to use two Views together and a small code snippet to make this work. It's a bit complicated but I will be able to walk you through it and help if you get stuck.
- Create a View of Department posts, filtered by your M2M relationship, and configure the Query Filter to select posts that are related to the post where this View is shown.
- In the Loop Output editor, insert a shortcode to display the post IDs of the Department posts, separated by a comma. Here's a snippet you can use as the contents of your Loop:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=1][wpv-post-id][wpv-item index=other],[wpv-post-id]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>[/wpv-no-items-found][wpv-layout-end]

- Insert the View of Department posts in the Single Staff Member Content Template or Template Layout. You should see a comma-separated list of the IDs of all the Departments related to the current Staff Member.
- Insert this code snippet in your child theme's functions.php file:

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

function ts_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 your View of Department posts.
- Create a new View of Staff Members, filtered by the M2M relationship, where the relationship is set using a shortcode attribute "wpvrelatedto".
- In the Loop editor, insert a post link shortcode to display a link to each Staff Member's single post.
- Insert the View of Staff Members on the Staff Members Content Template or Template Layout. Move the shortcode for the View of Departments into the wpvrelatedto shortcode attribute for the View of Staff Members, like this:

[wpv-view name="view-of-staff-members" wpvrelatedto="[wpv-view name='view-of-departments']"]

- Now your View of Staff Members is filtered by the list of Department IDs related to the current Staff Member.

#1096457

Just one question so far. I have ID of 34 outputting from the view. If I put 34 in for 12345 in functions.php, how does this ID change for each staff member (me, and the others you talked about above, each in our own department.)

Providing it can do that, this solution works quite nicely.

--Sam

#1096459

I tested it with myself and preliminary tests show that it is a success. Thank you. Will it work with any ID and not just the 34 I entered in functions.php?

#1097909

The number you place in functions.php should be the numeric ID of the View of Department posts. You can find this number by going to Toolset > Views, where you will see a list of all Views. The ID is the number in the left column.