Default value for Member type field in search form
-
Hi there, I’m using Member type field in my search form. I’m having two member types, men and women.
Would it be possible to have a default value for Member type field , based on the users Member type, to display search results for either same Member type or opposite member type?
Regards
Carsten
-
Hi Carsten,
Is your member type field a profile field (in Users -> Profile Fields) or the native BuddyPress member type field (in Users -> Member Types)?
Hi Andrea, thanks for your reply. I’m not sure that I understand the question because I use both. I have defined my two member types in in Users -> Member Types, and uses the Single Member Type field, in Users -> Profile Fields for profile setup?
View post on imgur.com
View post on imgur.com
Hi Carsten,
You can try adding this code to your bp-custom.php file:
add_action ('bps_field_before_search_form', 'set_default'); function set_default ($f) { if ($f->code == 'field_247') // replace 247 with your gender field ID { $user_id = bp_loggedin_user_id (); if ($user_id != 0) { $gender = xprofile_get_field_data (247, $user_id); // replace 247 with your gender field ID if ($gender == 'Male') $f->value = 'Female'; else if ($gender == 'Female') $f->value = 'Male'; } } }
Hi Andrea, thank you very much for the snippet, I have replaced the field id, and I have changed the member type in my language as well. But unfortunately I see no effect of the code. Members are mixed and the field in the search form has not set to opposite gender. Any idea what could be wrong?
Is it supposed to work every time the search form is loaded?
Regards
Carsten
Hi Carsten,
Sorry, I forgot you are using the Single Member Type field.
You have to use the Member Type ID and the Singular name of your member types, you can see them if you edit your member type in Users -> Member Types. Try changing the code as follows:
if ($gender == 'Male') // Singular name here $f->value = 'female'; // Member Type ID here else if ($gender == 'Female') // Singular name here $f->value = 'male'; // Member Type ID here
Please let me know if it works now!
Hi Andrea, on my site Singular name and Member Type ID are the same, Could it be why it is not working?
View post on imgur.com
View post on imgur.com
Hi Carsten,
No, that’s not a problem, but I can’t think of any new suggestions.
In these cases I’d offer to take a look at your site to better understand the issue, but that’s against the WordPress forum policy.
If you are interested, you can use my BP Profile Search Support Forum to continue this conversation:
Hi Andrea, thank you very much for the offer, just to be sure I made the right changes to the code, I paste it here:
add_action (‘bps_field_before_search_form’, ‘set_default’);
function set_default ($f)
{
if ($f->code == ‘field_4749’) // replace 247 with your gender field ID
{
$user_id = bp_loggedin_user_id ();
if ($user_id != 0)
{
if ($gender == ‘mand’) // Singular name here
$f->value = ‘kvinde’; // Member Type ID here
else if ($gender == ‘kvinde’) // Singular name here
$f->value = ‘mand’; // Member Type ID here
}
}
}Hi Carsten,
Your code looks fine to me. By the way, could you please paste your BP Profile Search debug info here? See this page for details:
Hi Andrea, sure.
BP Profile Search
Version5.4.8PlatformBuddyPress 11.3.1ThemeGeneratePress Child 0.1Members index template/themes/generatepress_child/buddypress/members/index.phpMembers loop template/plugins/buddypress/bp-templates/bp-nouveau/buddypress/members/members-loop.phpForm template(s)/plugins/bp-profile-search/templates/members/bps-form-default.php
Hi Carsten,
There is nothing unusual in the debug info. That’s good, but unfortunately it doesn’t help us in this case.
I have to follow up, I also have member types ‘Man’ and ‘Woman’ and I want the search for men to show women and for women men. It would be good if those fields were hidden and it searched for the opposite gender by default. If you could write the simplest code that does this, I would really appreciate it. Thanks in advance
Hi djapa89,
Yours is a different question. While Carsten needs a search field starting with a default value, you don’t need a search field at all.
You can add this code to your bp-custom.php file:
add_filter ('bps_hidden_filters', 'my_hidden_filters'); function my_hidden_filters ($filters) { $user_id = bp_loggedin_user_id (); if ($user_id != 0) { $gender = xprofile_get_field_data (247, $user_id); if ($gender == 'Male') $filters['field_247'] = 'Female'; else if ($gender == 'Female') $filters['field_247'] = 'Male'; } return $filters; }
Don’t forget to replace 247 with your actual gender field ID.
Can you explain please, because this code is not working for me. Do I need the field Genders in Users/Profile Fields for this or are only Users/Member types sufficient. I have Member Types
Member Type ID – female
Singular name – womanMember Type ID – man
Singular name – ManI want the members page not to show people of the same sex, only of the opposite sex. For men women and for women men.
Hi djapa89,
If you are using member type instead of a profile field, your code becomes:
add_filter ('bps_hidden_filters', 'my_hidden_filters'); function my_hidden_filters ($filters) { $member_type = bp_get_member_type (bp_loggedin_user_id ()); if ($member_type == 'teacher') // use Member Type IDs $filters['bp_member_type'] = get_term_by ('name', 'student', 'bp_member_type') -> term_id; else if ($member_type == 'student') $filters['bp_member_type'] = get_term_by ('name', 'teacher', 'bp_member_type') -> term_id; return $filters; }
Replace ‘teacher’ and ‘student’ as needed, always using the Member Type IDs.
- The topic ‘Default value for Member type field in search form’ is closed to new replies.