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/