Skip Navigation

[Resolved] display custom field in Cred form select belong to parent id instead %%NAME%%

This support ticket is created 7 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
- 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)

Author
Posts
#576324
Screen Shot 2017-10-04 at 11.07.01 PM.png
Screen Shot 2017-10-04 at 11.06.18 PM.png
Screen Shot 2017-10-04 at 11.06.09 PM.png
Screen Shot 2017-10-04 at 11.05.45 PM.png

I am trying to: select a parent for a custom post while in frontend cred form

Link to a page where the issue can be seen: it's on localhost. How to give you an access

I expected to see: option to replace %%NAME%% with another custom field associated with that cpt

Instead, I got: I need help with the same select option in setting a parametric search filter based on selecting parent > child

#576457

Noman
Supporter

Languages: English (English )

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

Hi Ahmed,

Thank you for contacting Toolset support. You can achieve this using custom code.

1. Please add this code in your theme’s or child theme’s functions.php file:

/**** Create custom shortcode [current_parent_post_select] ****/
function current_parent_post_select_func(){
    
    $args = array(
        'post_type' => 'parent_post_type_slug', // parent post type slug
        'nopaging' => true
    );
    // Return an array of all posts of current user
    $parent_posts_list = get_posts($args);
 	
	$content = '<select name="post_parent" class="form-control" output="bootstrap" required="required">';
			$content .= '<option>--- Choose your add ---</option>';
			foreach ($parent_posts_list as $post) :
				$custom_field_value = get_post_meta( $post->ID, 'wpcf-custom-field-slug', true );
				// 'wpcf-custom-field-slug' will be your custom field slug
				$content .='<option value=' . $post->ID . '>' . $custom_field_value . '</option>';
			endforeach;
	$content .='</select>';
	wp_reset_postdata();
	return $content;
}
add_shortcode( 'current_parent_post_select', 'current_parent_post_select_func' );

2. After that please use following shortcode in the form:

[current_parent_post_select]

3. Then saved parent ID on form submit using cred_save_data hook and this code:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

add_action('cred_save_data', 'prefix_cpt_cred_save_data_action', 10, 2);
function prefix_cpt_cred_save_data_action( $post_id, $form_data ){
    if ($form_data['id']==62893){ //do the following code only if the CRED form ID is 62893, adjust this to your CRED form ID
        if (!empty($_POST['post_parent'])){ //if parent is selected
            $parent_id = $_POST['post_parent'];
            add_post_meta($post_id, '_wpcf_belongs_parent-cpt-slug_id', $parent_id, true); //save the ID of the event as parent of the current post
			// _wpcf_belongs_parent-cpt-slug_id, If your parent cpt slug is 'event' then it will be
			// _wpcf_belongs_event_id
        }
    }
}

Please remember to carefully change the slugs and Form ID in above code.

Please open a new ticket for this 2nd issue (parametric search) and we would be happy to help. This will help other users with similar problems to find solutions when searching the forum, We do not handle multiple issues in the same ticket as per support policy. https://toolset.com/toolset-support-policy/

Thank you

#576517

Hello Noman,

Thank you very much for your reply.
I'll try to describe the issue as clear as possible.

1- I have a cred form to create new product name, then I have other cred forms to edit that product and complete filling the designed information.

2- The product has a parent [Region] which is a child of [Country] where I plan to select it first in order to get only it's child [Regions] and select the parent [Region] for my product.

3- I'm setting some conditions to check before choosing which post field to display in place of %%NAME%% in the select menu

4- Will I be able to duplicate your 2 actions to apply on [Country] & [Region] parents? Or there is another way?

Thank you,

Ahmed

#576558

Hello Noman,

By the way, The list doesn't remember the current parent selection if it was set before.

#576648

Noman
Supporter

Languages: English (English )

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

Hello Ahmed,

Thanks for explanations and making it clear. I am afraid this won't be doable using built-in options of Toolset. This may be possible using custom coding. The only hints I can think of are as below:

-- Here is ajax method to achieve this:
hidden link

-- Here is another method using js:
We can display 2 dropdowns 1 for country and other for regions and hide regions dropdown using css for now. Here is an example of country dropdown option.

<option value="country1">country 1</option>
<option value="country2">country 2</option>

We can use country post slug as region option class i.e

<option value="region1" class="country1">country1 Region 1</option>
<option value="region2" class="country1">country1 Region 2</option>
<option value="region3" class="country2">country2 Region 1</option>

When user select country dropdown, we will get selected option value and show only those options using class selector in region dropdown and hide others.

In this way, user can only see specific country regions.

This falls into Custom coding & custom development and it is out of support policy (https://toolset.com/toolset-support-policy/). So we recommend to contact Toolset recommended service providers to further discuss the custom approach. We have some recommended list of service providers here if you would like to take a look: https://toolset.com/consultant/

Thanks

#576651

Noman
Supporter

Languages: English (English )

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

Here is 2nd (JS) method implemented that I told above, you can see it in this sample HTML file I have created:
hidden link

#578429

Sorry for the delay, I’ll get back within a day

#578455

Noman
Supporter

Languages: English (English )

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

No problem, take your time and give it a try.

Have a great day.
Thank you