Skip Navigation

[Resolved] Display posts in relationship with multiple parents/childhs

This support ticket is created 3 years, 1 month 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
- 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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by cassianS 3 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#2176655

Tell us what you are trying to do?
- I have a structure of custom posts similar to the following one.
There are Projects (post types) and there are teams (post type), and team members (post type). Teams may work on different projects. Team members may work on different projects as well. But team member belongs only to one team.
I need to display on the Project page related teams with team member who are working on the project in the format:
- Team A
- - team member A
- - team member B

- Team B
- - team member C
- - team member D

So far I can manage to create view of teams, related to the project, and team members related to a team. But I need somehow to pick team members who related to both: team and project.
That's where I would need your advice.

Is there any documentation that you are following?
- I can't find anything related. One ticket was close to this: https://toolset.com/forums/topic/displaying-child-posts-of-a-selection-of-parents-in-an-unconnected-parent-post/
- Maybe PHP filter could work as well, but not sure: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Is there a similar example that we can see?
- nope, sorry. I can't find any

What is the link to your site?
hidden link

#2176989

Hi,

Thank you for contacting us and I'd be happy to assist.

For what you're trying to achieve, there is no straightforward method available, so it will require some workaround.

You can introduce a new post type "Team Groups" which will act as a subset of the larger group "Teams".

The "Projects" post type will have a one-to-many relationship with the "Team Groups" post type.

The "Teams" post type will also have a one-to-many relationship with the "Team Groups" post type.

The "Team Groups" post type will have a many-to-many relationship with the "Team Members" post type.

As a result, each "Team Group" post will give you information about, which "project" this team group is working on, which "team's" team members are included, and also the specific "team members" who are working.

On the single project page, you'll be able to show all the related "Team Groups" posts using a post view, to show the working "Team members", grouped by their "Teams".

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2177563

Hi Waqar, thank you for your response and suggestion.
May I ask you: what information "Team Groups" would have? Would it be a duplicate basically of the "Team member" post or it would be just a "connector" post?

Would "Team Groups" work as a taxonomy of "Projects" and "Team member" as well?

Is there any other options, maybe, some sort of custom PHP filter to make it work without "Team groups"? I am just thinking that having another post type could be difficult in terms of maintenance when "Team member" could change a "Team" for example or "Project", especially if all changes happen through Toolset Forms on the front end.

#2178697

Thanks for writing back.

> May I ask you: what information "Team Groups" would have? Would it be a duplicate basically of the "Team member" post or it would be just a "connector" post?

- In my proposed structure, the new "Team Groups" post type would only be used to connect other posts.

> Would "Team Groups" work as a taxonomy of "Projects" and "Team member" as well?

- No, "Team Groups" will only be a custom post type and it won't work as a taxonomy.

> Is there any other options, maybe, some sort of custom PHP filter to make it work without "Team groups"?

- There is another alternative, in which you can store each working team member's post ID in a repeating custom field, directly with the "Projects" post type.

The structure would look like this:

1. Post Types:
a). Projects
b). Teams
c). Team Members

2. Relationships
a). Projects <-> Teams: Many to many
b). Teams -> Team Members: One to many

3. Custom Field:
a). Project Team Members:
Field Type: Numeric
Slug: project-team-members
Attached to post type: Projects
Repeating/Multiple instances: Yes

After creating a "Project" post and connecting the related "Teams" post to it, you can show a new post edit form "Form to edit Project Team Members" on the single "Project" page, which will be set to edit the current "Project" post.

This form will only have a "Checkboxes" type generic field and the submit button. In the generic field's settings, you can use the same field slug "project-team-members" and set it to get the options dynamically from a custom shortcode "[populate_team_members_options]".
( screenshot: hidden link )

Next, you'll need a custom shortcode that first gets the related teams from the current project and then get the team members connected to only those teams.

Example:
( feel free to change the post type and relationships slugs as per your website )


add_shortcode('populate_team_members_options', 'populate_team_members_options_func');
function populate_team_members_options_func() {
	// get current project's ID
	$current_project_ID = get_the_ID();

	// get team members which are already assigned
	$current_project_existing_members = types_render_field("project-team-members", array("item" => $current_project_ID, "separator" => ','));
	$current_project_existing_members_arr = explode(',', $current_project_existing_members);

	// slugs of post types and relationships
	$team_member_cpt_slug = 'team-member';
	$project_team_rel_slug = 'project-team';
	$team_team_member_rel_slug = 'team-team-member';

	// get teams from current project
	$get_related_teams = toolset_get_related_posts( $current_project_ID, $project_team_rel_slug, 'parent', 99999, 0, array(), 'post_id', 'child' );

	// get team members from those teams and return them in a JSON format for the generic field
	foreach ($get_related_teams as $key => $value) {
		$query = new WP_Query( 
			array(
				'post_type' => $team_member_cpt_slug,
				'posts_per_page' => -1,
				'toolset_relationships' => array(
					'role' => 'child',
					'related_to' => $value,
					'relationship' => $team_team_member_rel_slug
				)
			)
		);
		$results = $query->posts;
		foreach ($results as $result) {
			// check if this team member is already assigned
			if(in_array($result->ID, $current_project_existing_members_arr)) {
				// if already assigned add 'default' => 'true' so that it's checkbox is checked
				$data[] = array('value' => $result->ID, 'label' => $result->post_title, 'default' => 'true' );
			}
			else
			{
				$data[] = array('value' => $result->ID, 'label' => $result->post_title );
			}	
		}
		
	}
	return trim(json_encode($data), '[]');

}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

This shortcode will not only generate the related team members as options, but will also automatically check the options for any team members which are already selected.

In the last step, you'll need a custom function attached to the hook "cred_save_data" ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data ), which can detect which team member options are checked when this form is submitted and updates the custom field values accordingly.

Example:


add_action('cred_save_data', 'custom_team_member_processing',10,2);
function custom_team_member_processing($post_id, $form_data)
{
	// if a specific form
	if ($form_data['id']==1234)
	{
		// delete all the team members from the custom field
		delete_post_meta($post_id, 'wpcf-project-team-members');

		// if any team member's checkbox is checked save its ID in the field
		if(!empty($_POST['project-team-members'])) {
			foreach ($_POST['project-team-members'] as $key => $value) {
				add_post_meta( $post_id, 'wpcf-project-team-members', $value );
			}
		}
	}
}

Please replace 1234 with your actual form's ID. As a result, the post IDs of all the selected team members will be available as custom field values in this new numeric field.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2178841

This is great! Thank you, Waqar. I will work on this now