This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Problem in User Custom Field

#17230
Ernest MarcinkoErnest Marcinko
Keymaster

Hi Bruno,

I think a single text field would be much more simpler – but this gave me an even better idea.

How about using a custom code in a hook, so when the user registers/edits it’s profile, it would create a ‘hidden’ meta field. Into that meta field the code would aggregate all the information you need for searching. On the plugin back-end, you would only need to select this field, and nothing else.

I have made a sample skeleton code:

[php]add_action( ‘profile_update’, ‘asp_additional_data_profile_update’, 10, 1 );
add_action( ‘user_register’, ‘asp_additional_data_profile_update’, 10, 1 );
function asp_additional_data_profile_update( $user_id ) {
$meta_value = "";
$meta_key = ‘_usearch_data’;

// Gather all the search data into the $meta_value variable

update_user_meta( $user_id, $meta_key, $meta_value);
}[/php]

This hook should trigger whenever a profile is added/edited. So in the commented section, you should aggregate all the string data you need into the $meta_value variable. Using the $user_id variable, you can get the user first:

[php]$user = get_userdata($user_id);[/php]

Then, as you have the $user variable, that includes the name and all that. So if you need the first and last nem, you could do:

[php]$meta_value .= ‘ ‘.$user->first_name.’ ‘.$user->last_name;[/php]

Then get any meta you want:

[php]$val = get_user_meta($user_id, ‘field_name’, true);
if ( $val !== false )
$meta_value .= ‘ ‘.$val;[/php]

..and in the end you will get all the information in one field. Then all you need is to choose this field on the back-end of the search: https://i.imgur.com/EbscHA0.png
All the other fields you can disable.

I hope this helps 🙂