Filter Dynamic field by user meta

By: Viktor D | Asked: 06/18/2023
ForumsCategory: Code SnippetsFilter Dynamic field by user meta
Viktor D asked 2 years ago

I found this code below on this website:   https://formidableforms.com/knowledgebase/frm_setup_new_fields_vars/#kb-add-user-dropdown   But for some reason, it doesn't work. The ID of the Dynamic field is 265, and the field that it's filtering is 147. I have a current user logged-in that belongs to a specific church per user meta field 'church_name'. I need the Dynamic field to filter users that only belong to that church. Can someone please check the code and let me know if I'm missing something? Thank you very much!  

add_filter( 'frm_setup_new_fields_vars', 'filter_dfe_options_by_user_meta', 25, 2 );
add_filter( 'frm_setup_edit_fields_vars', 'filter_dfe_options_by_user_meta', 25, 2 );
function filter_dfe_options_by_user_meta( $values, $field ) {
	if ( $field->id == 265 && ! empty( $values['options'] ) ) {//Replace 265 with the ID of your Dynamic field
		$temp                = $values;
		$temp['form_select'] = 147;//change 147 to the id of the field (in linked form) that you want to filter by
		$field2_opts         = FrmProDynamicFieldsController::get_independent_options( $temp, $field );
		$user_id             = get_current_user_id();
		if ( ! $user_id ) {
			$values['options'] = array();
			return $values;
		}
		$user_meta = get_user_meta( $user_id, 'church_name', true );//replace 'church_name' with the key of the user meta you want to filter by
		if ( ! $user_meta ) {
			$values['options'] = array();
			return $values;
		}
		foreach ( $values['options'] as $id => $v ) {
			if ( isset( $field2_opts[ $id ] ) && ( $v == '' || $field2_opts[ $id ] == $user_meta ) ) {//Only include values where filtering field equals Yes
				continue;
			}
			unset( $values['options'][ $id ] );
		}
	}
	return $values;
}
1 Answers
Victor Font answered 2 years ago

The first if statement tests for field id and not empty empty( $values['options'] ). If $values['options'] is empty, nothing will work. What are the contents of $values['options']? What is field 265? Is it a list of users or churches? What value is in field 147?

Viktor D replied 2 years ago

265 is the ID of the Dynamic Field. This 265 Dynamic field is linked to a form that collects user emails with field id 147. The user meta 'church_name' has a list of churches.

A logged-in person who belongs to a specific church based on user_meta 'church_name' should see only the users that belong to the same church in the dynamic field.

When the above code is deactivated, all of the users show up for all churches. When I activate the code, the dynamic field does not show any users.

Thanks for your help Victor.

Victor Font replied 2 years ago

Field 265 is a dynamic field that links to a form with field 147 on that form. Field 147 is an email field. How do you populate the email addresses in the dynamic form? What other fields are on the dynamic form?

How are linking the email addresses to the church? Is there a church field on the same form linked to the dynamic field that displays user emails?

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right