Skip Navigation

[Resolved] CRED – parent-child drop-down relationship

This support ticket is created 7 years, 3 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 19 replies, has 2 voices.

Last updated by Bochnacki 7 years, 2 months ago.

Assisted by: Noman.

Author
Posts
#553789

What is the best way to do the same thing in parent-child selectors in the search filter?
So: I have two drop-downs - "brand" and "model" -> I choose a brand and in the drop-down "model" I only have models that are the children of the brand previously selected.
I know that in CRED forms it does not work the same as in search filter category views.

My drop-down setting in CRED form:

[cred_field field='_wpcf_belongs_osobowe-marka_id' value='' order='date' ordering='desc' required='true' select_text='--- Select osobowe-marka ---' validate_text='osobowe-marka must be selected' class='form-control' output='bootstrap']
[cred_field field='_wpcf_belongs_osobowe-model_id' value='' order='date' ordering='desc' no_parent_text='No Parent' class='form-control' output='bootstrap']
[cred_field field='_wpcf_belongs_osobowe-wersja_id' value='' order='date' ordering='desc' no_parent_text='No Parent' class='form-control' output='bootstrap']

I will be grateful for the advice and suggestions.

#553826

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting Toolset Support.

What is the best way to do the same thing in parent-child selectors in the search filter?
==> Do you want to create this relationship between fields in the CRED form or Search Filter form?

In the CRED shortcode you have provided above, you may try to add parent field slug in the no_parent_text='No Parent' attribute.

Thank you

#553847

I want to create this relationship between fields in the CRED form.

"In the CRED shortcode you have provided above, you may try to add parent field slug in the no_parent_text='No Parent' attribute." - I have tried, unfortunately, nothing changes. Only displays another text.

#553879

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Ok, please try the solutions posted here, this looks same as your case:
https://toolset.com/forums/topic/conditional-display-of-taxonomies-in-cred-form/

Conditional Display for Form Inputs:
https://toolset.com/documentation/user-guides/conditional-display-for-form-inputs/

Advanced Conditional Display for Form Sections:
https://toolset.com/documentation/user-guides/cred-conditional-display-engine/

#553891

Unfortunately none of these examples will help me.
In each of these cases, you need to insert specific values. In my case they are dynamic - models are to be shown depending on which brand I choose.

#553902

I found this thread: https://toolset.com/forums/topic/cred-two-parent-types-fields-content-of-first-changes-content-of-second/page/2/#post-258735
I'm trying to apply it, but I guess I'm mistakenly typing my CPT names.
Besides, I do not know how to add a third select.

My CPT names (in order: parent -> child -> grandson):
osobowe-marka
osobowe-model
osobowe-wersja

Can you help me write this properly?

#553936

I changed the CPT names to no problem with "-".
Now the names are:
osobowemarka
osobowemodel
osobowewersja

Code from thread https://toolset.com/forums/topic/cred-two-parent-types-fields-content-of-first-changes-content-of-second/page/2/#post-258735

//Create parent/child relationship between listing and province/city
add_action('cred_submit_complete', 'prefix_my_cred_submit_complete_action', 10, 2);
function prefix_my_cred_submit_complete_action( $post_id, $form_data ){
    if ($form_data['id']==1337){ //do the following code only if the CRED form ID is 1337, adjust this to your CRED form ID
        if (!empty($_POST['wpcf-listing_province'])){ //if listing-province has a value
            $province_id = get_post_meta( $post_id, 'wpcf-listing_province', true );
            add_post_meta($post_id, '_wpcf_belongs_province_id', $province_id, true); //save the ID of the province as parent of the current post
        }
    }
}

My code:

//Create parent/child relationship between listing and province/city
add_action('cred_submit_complete', 'prefix_my_cred_submit_complete_action', 10, 2);
function prefix_my_cred_submit_complete_action( $post_id, $form_data ){
    if ($form_data['id']==39){ //do the following code only if the CRED form ID is 1337, adjust this to your CRED form ID
        if (!empty($_POST['wpcf-osobowemarka'])){ //if listing-province has a value
            $osobowemarka_id = get_post_meta( $post_id, 'wpcf-osobowemarka', true );
            add_post_meta($post_id, '_wpcf_belongs_osobowemarka_id', $osobowemarka_id, true); //save the ID of the province as parent of the current post
        }
    }
}

The problem may be that I do not have a "listing" ?...
I do not know very well PHP.

#554011

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Ok, I am checking the old thread and info / code that you have provided and will get back to you with my findings. Thanks

#554022

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

In the old example thread, this is basically 3 steps process:

1. Add select Dropdown options in your CRED form (this method can’t be used with builtin CRED form fields or generic fields), as explained here:
https://toolset.com/forums/topic/cred-two-parent-types-fields-content-of-first-changes-content-of-second/#post-245184

Rough example something like:

<select name="cpt-son">
	<option value="[wpv-post-id id="$son"]" data-state="[wpv-post-title id="$parent"]">Son 1</option>
	<option value="[wpv-post-id id="$son"]" data-state="[wpv-post-title id="$parent"]">Son 2</option>
</select>
 
<select name="cpt-grandson">
	<option value="[wpv-post-id id="$grandson"]" data-state="[wpv-post-title id="$son"]">Grandson 1</option>
	<option value="[wpv-post-id id="$grandson"]" data-state="[wpv-post-title id="$son"]">Grandson 2</option>
</select>

2. Write your own Custom Javascript code to update (show/hide) the select dropdown fields based on the selected values.

3. Then use the php code given here in theme’s functions.php file(as you have provided):
https://toolset.com/forums/topic/cred-two-parent-types-fields-content-of-first-changes-content-of-second/page/2/#post-258735

Rough example something like:

add_action('cred_submit_complete', 'prefix_my_cred_submit_complete_action', 10, 2);
function prefix_my_cred_submit_complete_action( $post_id, $form_data ){
	if ($form_data['id']==39){ //do the following code only if the CRED form ID is 39, adjust this to your CRED form ID
        	
    	if (!empty($_POST['cpt-son'])){ //if listing-son(Parent CPT Custom Dropdown) has a value
        	$son_id = $_POST['cpt-son'];
            add_post_meta($post_id, '_wpcf_belongs_son_id', $son_id, true); //save the ID of the son as parent of the current post
    	}
                    	
                    	if (!empty($_POST['cpt-grandson'])){ ////if listing-grandson(Parent CPT Custom Dropdown) has a value
            $grandson_id = $_POST['cpt-grandson'];
            add_post_meta($post_id, '_wpcf_belongs_grandson_id', $grandson_id, true); //save the ID of the grandson as parent of the current post
    	}
                    	
                    	
	}
}

Please note there will be more or less modifications needed in this code and JS. Because example is given for 2 post types and you have 3 post types which adds complexity to the requirement. It will be a lot more custom work and we can’t help for such. Sorry, it will require more custom development to make it work perfectly and we cannot consult on the custom code as per the support policy. https://toolset.com/toolset-support-policy/

We have some recommended list of service providers here who can help you in such case: https://toolset.com/consultant/

Thanks

#554355
<select name="cpt-son">
    <option value="[wpv-post-id id="$son"]" data-state="[wpv-post-title id="$parent"]">Son 1</option>
    <option value="[wpv-post-id id="$son"]" data-state="[wpv-post-title id="$parent"]">Son 2</option>
</select>

Any item (Son 1, Son 2,...) must be entered manually? There is no other way?
Items in all categories I have over 4000 ...

#554368

2. Write your own Custom Javascript code to update (show/hide) the select dropdown fields based on the selected values.
I do not want to offend you, but this way I might as well insert a list of "parents" and solve the problem with Java Script ???? If I knew Java Script...
I am afraid that the proposed solution is not possible for me.

#554633

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

If this is not possible for you to enter the values manually then you can create a loop for this. From here you would need to get an idea for syntax to create a loop:
https://toolset.com/forums/topic/cred-two-parent-types-fields-content-of-first-changes-content-of-second/#post-245184

This is a custom solution and unfortunately I cannot consult more on this custom requirement, because we can't consult much on the custom features and coding. The method and approach I have provided in previous response was to provide a general idea for a possible solution.

Thanks

#555778

Ok, So I would like to use JavaScript solution to filter other selects on change the others ... but to do so, I will need those select options to have some useful data arguments ... for now, I have option output like that:

<option class="wpt-form-option form-option option option" value="2906" data-wpt-type="option" data-wpt-id="cred_form_39_2_cred_form_39_2-select-3-1501692394" data-wpt-name="_wpcf_belongs_osobowe-wersja_id">GLA 1</option>

With useful data in there I have it's ID, and it's name ... nothing more. I would need it's "Post relationship" settings to display in there, like data-belongsto-whateverposttype="that_post_type_post_ID" ... how can I do it?

Also from previous response, I supposed to understand loop syntax from this?

<select name="city">
<wpv-loop><option data-state="[wpv-post-title id="$state"]">[wpv-post-title]</option></wpv-loop>
</select>

Sorry, I did not, perhaps we know different definitions for loops.
Do you have some documentation page for loops syntax in your code?

#555967

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

I would need it's "Post relationship" settings to display in there, like data-belongsto-whateverposttype="that_post_type_post_ID" ... how can I do it?
>> You need to create shortcode in functions.php and add the shortcode in your Cred form.

For Loop, please go through this documentation: https://toolset.com/documentation/user-guides/digging-into-view-outputs/
Shortcode: https://codex.wordpress.org/Shortcode_API

Thank you

#556239

Thanks Noman, is there no other way than create my own shortcode? Can I filter output od your cred_field shortcode? Or perhaps you have some hooks in there?

If not, can you give me some hint where can I find it's code, so I could just copy it and modify as own shortcode instead of writing everything from scratch. Also how can I grab post relations, do you have some functions for that, or should I grab it manually from post meta?