Hi,
I have two cpt's from which one the the parent of the other.
In the child form (to add a new entry), there is dropdown for the parent but it shows all possible candiates and I only want the parents that were created by the current user.
Second question : is there a way to limit a number field to an integer from 0 to 70 ? In the cred ?
Thx
Bart
Dear Bart,
Q1) In the child form (to add a new entry), there is dropdown for the parent but it shows all possible candiates and I only want the parents that were created by the current user.
There isn't such a built-in feature within CRED form, but you can try to change it with CRED filter hook "wpml_cred_potential_parents_filter", for example, in your theme/functions.php, add below codes:
add_filter('wpml_cred_potential_parents_filter', 'my_func', 10, 4);
function my_func($parents, $wpml_name, $wpml_context, $args){
$current_user_id = get_current_user_id();
if($wpml_name == '_wpcf_belongs_parent-cpt_id' && $current_user_id){ // replace "parent-cpt" with the parent post type slug
$args['author'] = $current_user_id;
$parents = get_posts($args);
}
return $parents;
}
please replace "parent-cpt" with the parent post type slug
Q2) is there a way to limit a number field to an integer from 0 to 70 ?
Same as above, there isn't such a built-in feature within CRED form, but you can try with filter hook "cred_form_validate", check if the value submitted by user, if the field value isn't an integer from 0 to 70, then return an error, see our document:
cred_form_validate
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
DescriptionThis hook provides custom validation for form fields
Ok thx. This I can use off course.
But how can I limit the scope of this filter ? It now applies to all parents.
Can I limit it per form ?
Yes, it is possible, for example, you can modify the PHP codes as below:
add_filter('wpml_cred_potential_parents_filter', 'my_func', 10, 4);
function my_func($parents, $wpml_name, $wpml_context, $args){
global $wp_query;
$current_user_id = get_current_user_id();
$cred_form_ids = array(123456); // here setup the specific CRED form IDs
$arr = explode('-', $wpml_context);
$end = end($arr);
if($wpml_name == '_wpcf_belongs_parent-cpt_id' && $current_user_id && in_array($end, $cred_form_ids)){
$args['author'] = $current_user_id;
$parents = get_posts($args);
}
return $parents;
}
Please replace 123456 as your specific CRED form IDs