I need the following functionality:
Current user should not given the ability to create an answer for a sample, he/she has already created. This way, select inputs would become less and less, until zeroed.
So, for each item of the select input: Show parent post, if there is no child created by the current user.
Solution:
We need to add following code in theme’s functions.php file. When user submits CRED form, it will get sample post ID from our custom dropdown and save it as parent in the submitted post using cred_save_data hook.
add_action('cred_save_data', 'prefix_my_cred_cred_save_data_action', 10, 2);
function prefix_my_cred_cred_save_data_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['newparent'])){ //if parent is selected
$parent_id = $_POST['newparent'];
add_post_meta($post_id, '_wpcf_belongs_sample_id', $parent_id, true); //save the ID of the sample as parent of the current post
}
}
}
==> Replace ‘39’ with your CRED form id.
==> Please go through above code, you might also want to change something else in that (field name, etc).
1. You can add your shortcode directly in cred form.
<div class="form-group">
<label>Give your answer</label>
[custom_new_sample_select]
</div>
2. If you find any issue then please don’t do directly ‘echo’ in your custom shortcode. First save them in some variable and then use return statement as follows.
Following Code:
I am building a membership site and I use two CPTs, samples and answers which are children of samples.
I have a cred form, for the current user to create answers and this select input is used to choose the parent:
Everything works fine, but I need the following functionality:
The select input should not list parents for which children are already made by the current user. In other words, current user should not given the ability to create an answer for a sample, he/she has already created. This way, select inputs would become less and less, until zeroed.
So, for each item of the select input: Show parent post, if there is no child created by the current user.
Thank you for contacting Toolset support. Using builtin Toolset options this might not be directly possible to do. So to achieve this you would need to make a custom shortcode to add a Custom parent dropdown in the CRED form and use our CRED API to save the data.
In the custom shortcode we need to define following functionalities:
1. We need to get current user id https://developer.wordpress.org/reference/functions/get_current_user_id/
2. Then we need to get all child (answers) posts of above user https://codex.wordpress.org/Class_Reference/WP_Query
3. And then get all parent ids from above results and store in array.
4. After that we need to get all parent (Sample) post except above store ids.
5. We will make custom select/option dropdown using above Sample posts.
6. Now we have just those Samples that are not created by current user.
1. If I make a custom shortcode to add a Custom parent dropdown (as you described) every time the CRED form is loaded, then why I need to use the "cred_save_data" filter hook?
2. Where is the documentation on how to make a custom shortcode?
Please note that above info is specially for someone having WP programming experience. And that we cannot consult on custom code as this is out of support policy (https://toolset.com/toolset-support-policy/). We only support builtin features of Toolset. For this, you can 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/
We need to add following code in theme’s functions.php file. When user submits CRED form, it will get sample post ID from our custom dropdown and save it as parent in the submitted post using cred_save_data hook.
add_action('cred_save_data', 'prefix_my_cred_cred_save_data_action', 10, 2);
function prefix_my_cred_cred_save_data_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['newparent'])){ //if parent is selected
$parent_id = $_POST['newparent'];
add_post_meta($post_id, '_wpcf_belongs_sample_id', $parent_id, true); //save the ID of the sample as parent of the current post
}
}
}
==> Replace ‘39’ with your CRED form id.
==> Please go through above code, you might also want to change something else in that (field name, etc).
1. You can add your shortcode directly in cred form.
<div class="form-group">
<label>Give your answer</label>
[custom_new_sample_select]
</div>
2. If you find any issue then please don’t do directly ‘echo’ in your custom shortcode. First save them in some variable and then use return statement as follows.
Excellent, thank you Noman!
The replacing of the 'echo' did the trick!
One last question please:
Is there an easy way to make the newparent field required?
I do not want the users to submit the form with an empty newparent field.
For further questions or issues, please kindly open a new ticket for each. This will help other users with similar problems to find solutions when searching the forum. You can assign those tickets to me directly if you wish to.