Skip Navigation

[Resuelto] CRED: parent-child drop-down relationship

This support ticket is created hace 6 años, 9 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 8 respuestas, tiene 2 mensajes.

Última actualización por Christian Cox hace 6 años, 9 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#612980

Hi, I try to do the following.

I have four custom post types, "contact", "team", "members" and "task".

I have created a many-to-many relationship between contact and team (members), so in this way, in "members", I store the contacts that belong to a team, since a team can have many members and a contact can be a member of many teams.
Then I have the custom post types "homework", where I store the tasks that will then be assigned to a member of a team. Between "team" and "homework" I have a one to many relationship, since a team can have many tasks.

The theme is the following:

I have a cred form "new task" where I create a task and assign it to a "member" of a particular "team". To do this, within the form I try to show two selectable selections, one with "team" and another with its "member" where I first want to select the "team" and then in the other drop-down select to show all the members of the team selected before.

I know that I can create a view to fill the drop-down selections, but I can not understand how to show in the drop-down select only the members that belong to the previously selected team in the previous drop-down select.

In the cred "tasks" form I already have the drop-down select with its parent "team" (_wpcf_belongs_team_id)

Can you help me with this?

#613055

Hi, the solution in CRED involves multiple generic fields and some CRED conditionals. Let me explain:

- Create a generic "select" field for each Team. Call these fields something like "member-select-12345," where 12345 represents the Team's ID. Each Member's name should appear as an option in the generic field, and the value of each option should be the Member's numeric ID.

- Wrap the member-select generic fields in CRED conditional code that shows and hides them based on the value of the _wpcf_belongs_team_id parent field. If the selected team value is "12345", show the field "member-select-12345". If the selected team value is "67890", show the field "member-select-67890", and so on. More information about CRED conditionals here:
https://toolset.com/documentation/user-guides/conditional-display-for-form-inputs/

- Once you have these generic fields and conditionals implemented, you can use the CRED API to capture the values submitted and perform some action with those values. You can find the selected values in the $_POST superglobal. Example:

add_action('cred_save_data', 'set_task_member',100,2);
function set_task_member($post_id, $form_data) {
  $forms = [1234];
  if( in_array( $form_data['id'], $forms ) ) {
    $team = $_POST['_wpcf_belongs_team_id'];
    $member = $_POST['member-select-' . $team];
    // Do something with the team and member information 
    // For example, to make this post a child of the selected member, you could do something like this:
    update_post_meta( $post_id, '_wpcf_belongs_member_id', $member, true );
  }
}

Modify 1234 to match your CRED form ID, modify _wpcf_belongs_member_id to match the member cpt slug, and modify the other field slugs as needed. More info about this API here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/update_post_meta

#617883

Hi! Sorry for the delay in the answer, I'm picking up on this topic.

I understand the answer and I clarify why I do not know if my query was correctly understood.

The relations between the custom post types is the following:
- Tasks: Son of "Team"

- Team: Father of "Members"
- Contact: Father of "Members"
* Member: It is a appearances between "Team" and "Contact"

What I try to do is that when I create a new cred form "New Task", I have two drop-down select with the "appearances" (two select drop-down), where in the first select I can select a "Team" and in the second select can select the "Contacts" that belong to the team of the first select.

In the previous answer it was not clear to me if each drop-down select has to be filled with its own view.

Thanks!

#618062

In the previous answer it was not clear to me if each drop-down select has to be filled with its own view.
Yes, each generic field can be populated with a View for simplicity. Here's an outline that shows what you will create. Each - represents a result in a View:

Select team:
- Team 1 (value 1)
- Team 2 (value 2)
- Team 3 (value 3)
...etc...

CONDITIONAL - show if team = 1
Select member:
- Member A (value a)
- Member B (value b)
- Member C (value c)
END CONDITIONAL

CONDITIONAL - show if team = 2
Select member:
- Member D (value d)
- Member E (value e)
- Member F (value f)
END CONDITIONAL

CONDITIONAL - show if team = 3
Select member:
- Member G (value g)
- Member H (value h)
- Member J (value j)
END CONDITIONAL

In a View of Members, if you want to show the Contact name instead of the Member name, you can use id="$contact" in a Views shortcode to access information about the parent Contact:

[wpv-post-title id="$contact"]
[wpv-post-id id="$contact"]
#618454

Well, some doubts more.

1- The first selection of the team must result from the view of the type "team" or "member" ([wpv-post-id id="$team"])?

2- The only thing I want to get from the "team" -> "contact" appearance is to have the "id" of the contact selected so I can assign a task to the user with that id, so I think that in this case I would need to use cred_save_data. I do not want to save any of the data obtained in the select.

3- I can not see how to create the conditional in the contact select to show it in relation to the selected in the team select.

I could share an access to the admin so you can see with more certainty. My English is not the best and I am afraid that some terms will be confused by the interpretation of each language.

#618719

Okay I think seeing your admin will be helpful. I will activate private reply fields here so you can share login credentials.

#619498

I edited your CRED form and added the conditionals I was describing:

        
[cred_show_group if="($(team-select) eq  '868' )"  mode='none']
			<label>MIEMBROS:</label>
[cred_generic_field field='miembros' type='select' class='' urlparam='' class='form-control' output='bootstrap']
			{
			"required":0,
			"validate_format":0,
			"default":[],
			"options":[ [wpv-view name="vista-de-miembros" wpvprchildof="868"] ]
			}
			[/cred_generic_field]
[/cred_show_group]
          
[cred_show_group if="($(team-select) eq  '867' )"  mode='none']
			<label>MIEMBROS:</label>
[cred_generic_field field='miembros' type='select' class='' urlparam='' class='form-control' output='bootstrap']
			{
			"required":0,
			"validate_format":0,
			"default":[],
			"options":[ [wpv-view name="vista-de-miembros" wpvprchildof="867"] ]
			}
			[/cred_generic_field]
[/cred_show_group]
          
[cred_show_group if="($(team-select) eq  '864' )"  mode='none']
			<label>MIEMBROS:</label>
[cred_generic_field field='miembros' type='select' class='' urlparam='' class='form-control' output='bootstrap']
			{
			"required":0,
			"validate_format":0,
			"default":[],
			"options":[ [wpv-view name="vista-de-miembros" wpvprchildof="864"] ]
			}
			[/cred_generic_field]
[/cred_show_group]

Then I edited this View:
enlace oculto
I added a post relationship filter, set by a shortcode attribute. Please review the results and let me know if you have additional questions.

#619707

I had understood well then.

Two queries:
1- The result obtained in the "Members" select is correct. But I want it to behave dynamically, so if I want to add a new team, I do not have to add a new conditional output with the ID of each team.
2- In the first select "Teams", I want to obtain the list of teams in which the user is currently connected.

Thank you!

#619767

1- The result obtained in the "Members" select is correct. But I want it to behave dynamically, so if I want to add a new team, I do not have to add a new conditional output with the ID of each team.
I do not have a very good solution for this, unfortunately. Ideally, you would use a filtered View of Teams to output multiple cred_generic_field shortcodes dynamically, but we do not recommend it. The official recommendation is to manually create new generic fields for each team.

2- In the first select "Teams", I want to obtain the list of teams in which the user is currently connected.
Toolset isn't designed to filter a View of post type A by custom field values in a related post type B. This is a complex filter system that will require a significant amount of custom code with queries traversing related Teams, Members, Contacts and Users:
- Teams and Contacts are parents of Members
- Contacts are linked to Users by a custom field value in the Contact post

Child posts are related to parent posts using a hidden custom field, stored in the postmeta table, linked to the child post. The format of the field key is "_wpcf_belongs_posttypeslug_id", where "posttypeslug" should be replaced by the slug of the parent CPT. Types custom field values are stored in the postmeta table using the key format "wpcf-fieldslug", where "fieldslug" should be replaced by the slug of the field in wp-admin.

If you have trouble with any specific part of this code I will be glad to take a look and make suggestions. Here is some related documentation:
https://codex.wordpress.org/Class_Reference/WP_Query
https://developer.wordpress.org/reference/functions/get_post_meta/