Skip Navigation

[Resolved] parent dropdown must only show parents created by the current user

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

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 6 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#621823

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

#621975

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

#622280

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 ?

#622386

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