Title: Relationship between custom fields
Last modified: November 24, 2021

---

# Relationship between custom fields

 *  Resolved [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/)
 * Hello.
 * Here I am Again with hopefully the last question and then don’t disturb You agin
   for long time 🙂
 * Is there a way to connect 2 or more fields between them?
 * This is my Scenario, that i think is kind of typical:
 * People in the registration have one Radio (I am male/female) Then inside their
   Profile there should be a field dropdown (I am searching for a male/female). 
   Now Let’s suppose that a second user let’s say a male, want to search all the
   female searching a male. Or also only a female. How can be done? Thanks once 
   againe

Viewing 15 replies - 1 through 15 (of 32 total)

1 [2](https://wordpress.org/support/topic/relationship-between-custom-fields/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/relationship-between-custom-fields/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/relationship-between-custom-fields/page/2/?output_format=md)

 *  Plugin Contributor [Champ Camba](https://wordpress.org/support/users/champsupertramp/)
 * (@champsupertramp)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15102877)
 * Hi [@roberton1973](https://wordpress.org/support/users/roberton1973/)
 * You can try the following code snippet to make the member directory search for
   a user based on the selected gender `I am searching for a male/female`.
 *     ```
       add_filter('um_prepare_user_query_args', 'um_112521_search_for_male_or_female', 10, 2 );
       function um_112521_search_for_male_or_female( $query_args, $args ){
   
           $user_id = get_current_user_id();
           um_fetch_user( $user_id );
           $search_gender = um_user("search_for_male_or_female");
           $query_args['meta_query'][ ] = array(
               'relation' => 'OR',
               array(
                   'key'       => 'gender',
                   'value'     => '=',
                   'compare'   => $search_gender
               ),
           );
   
           return $query_args;
       }
       ```
   
 * With the code above, you need to change the `search_for_male_or_female` with 
   your meta name/key of `I am searching for a male/female` field. Ensure that the`
   I am searching for a male/female` has the exact `Male` and `Female` options same
   as the Gender field options.
 * Regards,
 *  Thread Starter [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15104842)
 * Hello.
 * Thank You for the answer.
 * So, if I put the code as You told me:
 *     ```
       add_filter('um_prepare_user_query_args', 'um_112521_search_for_male_or_female', 10, 2 );
       function um_112521_search_for_male_or_female( $query_args, $args ){
   
           $user_id = get_current_user_id();
           um_fetch_user( $user_id );
           $search_gender = um_user("gender_search");
           $query_args['meta_query'][ ] = array(
               'relation' => 'OR',
               array(
                   'key'       => 'gender',
                   'value'     => '=',
                   'compare'   => $search_gender
               ),
           );
   
           return $query_args;
       }
       ```
   
 * I got not results at all. eve without filtering, is simply empty the page of 
   the directory
 * Strangerly, ifi take your code without modificatrions, i can filter but whathever
   i do, i got as the result the same sex persons. Is like i see all the person 
   searching for example i am on the oflline site for the tests where i registered
   me and my wife. I put to me “I search for female” and for her i search for male.
   If i search for a female, it’s exiting my profile, the same happeningwith my 
   wife. If i put for her “i search for a male”, only her profile will appear. this
   happening only without changing nothing to yyour code.
 * if i change this line
 * `$search_gender = um_user("search_for_male_or_female");`
 * with:
 * `$search_gender = um_user("gender_search");`
 * Nothing at all appear.
 * I double checked th metakey name and the option. Everything is ok.
 * The only difference is that the gender field is a radio, while thegender_search
   is a dropdown.
 * Could be that?
    -  This reply was modified 4 years, 6 months ago by [roberton1973](https://wordpress.org/support/users/roberton1973/).
    -  This reply was modified 4 years, 6 months ago by [roberton1973](https://wordpress.org/support/users/roberton1973/).
    -  This reply was modified 4 years, 6 months ago by [roberton1973](https://wordpress.org/support/users/roberton1973/).
 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15105150)
 * [@roberton1973](https://wordpress.org/support/users/roberton1973/)
 * Switch two values in the array and the code snippet will give you the Member 
   Directory listings depending on the profile’s gender search settings.
 *     ```
       array(
                   'key'       => 'gender',
                   'value'     => $search_gender,
                   'compare'   => '='
               ),
       ```
   
 *  Thread Starter [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15105782)
 * [@missveronicatv](https://wordpress.org/support/users/missveronicatv/)
 * Thanks also to You for your kind answe. Sadly the results don’t change. I always
   got the message that there are not members, even without filtering.
 * I think I am wrong something here:
 *     ```
       $search_gender = um_user("gender_search");
           $query_args['meta_query'][ ] = array(
               'relation' => 'OR',
       ```
   
 * I tried also to put gender search in a single braket, but no results. `$search_gender
   = um_user('gender_search');`
 * Thanks again
 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15107539)
 * [@roberton1973](https://wordpress.org/support/users/roberton1973/)
 * Can you test to use this condition:
 * `'compare' => 'LIKE'`
 *  Thread Starter [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15107552)
 * [@missveronicatv](https://wordpress.org/support/users/missveronicatv/)
 * Thanks again. Sadly still not work.
 * I will put here again all the code with the LIKE correction:
 *     ```
       add_filter('um_prepare_user_query_args', 'um_112521_search_for_male_or_female', 10, 2 );
       function um_112521_search_for_male_or_female( $query_args, $args ){
   
           $user_id = get_current_user_id();
           um_fetch_user( $user_id );
           $search_gender = um_user("gender_search");
           $query_args['meta_query'][ ] = array(
               'relation' => 'OR',
               array(
                   'key'       => 'gender',
                   'value'     => $search_gender,
                   'compare'   => 'LIKE'
               ),
           );
   
           return $query_args;
       }
       ```
   
 * Thanks so much for the time you tooked to try to solve my problem
 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15107563)
 * [@roberton1973](https://wordpress.org/support/users/roberton1973/)
 * Can you copy the values users has to choose from for these fields and show us
   here in the forum:
 * gender
 * gender_search
 *  Thread Starter [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15107580)
 * [@missveronicatv](https://wordpress.org/support/users/missveronicatv/)
 * They are both set on
 * Žensko (Female)
    Muško (Male)
 * Both in this exact order.
 * Gender is set as radio, while gender_search as Dropdown. Maybe there is the problem?
 *  Thread Starter [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15107649)
 * [@missveronicatv](https://wordpress.org/support/users/missveronicatv/)
 * Sorry! I saw now that is Working, but in a different way that i was expecting.
   I wase put it in the profile I am searching for and then now, with the last correction
   i noticed later that i can see only the woman! that is a cool feature too, that
   i will take. Thanks. But it’s possible to set it as filter? that was my original
   question, and maybe if it would possible to add “Searching for both” like 3rd
   option to both profile and filter would be top!
 * Thanks & Regards.
 *  Thread Starter [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15116497)
 * Hi.
 * Any news on about ho to filter from gender directly in member directory?
 * Thanks
 *  Plugin Contributor [Champ Camba](https://wordpress.org/support/users/champsupertramp/)
 * (@champsupertramp)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15120201)
 * Hi [@roberton1973](https://wordpress.org/support/users/roberton1973/)
 * Unfortunately, you can only add the Gender filter to have Male and Female values.
   To add an extra option in the Gender filter, you can try the following code snippet:
 *     ```
       add_filter("um_member_directory_filter_select_options","um_120121_show_extra_gender", 10, 3);
       function um_120121_show_extra_gender( $options, $values_array, $attrs ) {
   
           if( "gender" == $attrs["metakey"] ){
               $options["both"] = "Both Female & Male";
           }
           return $options;
       }
       ```
   
 * And the following code snippet is needed to query the Gender for both Male and
   Female:
 *     ```
       add_filter('um_prepare_user_query_args', 'um_120121_search_for_male_and_female', 10, 2 );
       function um_120121_search_for_male_and_female( $custom_query_args, $args ){
   
           foreach( $custom_query_args['meta_query'] as $i => $m ){
               if( isset( $m[0]["key"] ) ){
                   if( "gender" === $m[0]["key"] && "Both Female & Male" == $m[0]["value"] ){
                       $custom_query_args['meta_query']["has_both_gender"] = true;
                       unset( $custom_query_args['meta_query'][ $i ]);
   
                   }
               }
           }
   
           if( isset( $custom_query_args['meta_query']["has_both_gender"] ) ){
               $custom_query_args['meta_query'][ ] = array(
                   'relation' => 'OR',
                   array(
                       'key'       => 'gender',
                       'value'     => "Male",
                       'compare'   => 'LIKE',
                   ),
                   array(
                       'key'       => 'gender',
                       'value'     => "Female",
                       'compare'   => 'LIKE',
                   ),
               );
   
               unset( $custom_query_args['meta_query']["has_both_gender"] );
           }
   
           return $custom_query_args;
       }
       ```
   
 * Regards,
 *  Thread Starter [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15120559)
 * Hi. thank You.
 * It does not seems to work for me.
 * When I go to member direcetroy, If choose that i search for a female, it displays
   only the persons searching for female profiles, instead to show all the women’s
   profile.
 * Insteat, if i Left only the code of Miss veronica, is filtering only because 
   set in the user profile. For example, if in my profile i choose to see only women
   in member directory, it will. I would like to do the same, but directly from 
   member directory, so user don’t need to have this field in the profile.
 * Thanks again
 *  Plugin Contributor [Champ Camba](https://wordpress.org/support/users/champsupertramp/)
 * (@champsupertramp)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15124261)
 * Hi [@roberton1973](https://wordpress.org/support/users/roberton1973/)
 * If you don’t need the `gender_search` field in the Profile Form, what’s the reference
   for the member directory to search gender based on the currently logged-in user’s
   interest? By default, the Gender filter in the Member Directory filters the Profiles
   by gender. If you search for females, it will show all females. Could you please
   clarify?
 * Regards,
 *  Thread Starter [roberton1973](https://wordpress.org/support/users/roberton1973/)
 * (@roberton1973)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15124515)
 * Hi.
 * I would like that the search for female or male is done from member directory
   and not set in the profile or alternatively, that would be possible to overwrite
   what choosen in the profile using the filter in member directory.
 * For example in the profile nothing is choosen and in that way will show both,
   then user go in member directory and choose whatever he/she wants.
 * Thanks and regards
    -  This reply was modified 4 years, 6 months ago by [roberton1973](https://wordpress.org/support/users/roberton1973/).
 *  Plugin Contributor [Champ Camba](https://wordpress.org/support/users/champsupertramp/)
 * (@champsupertramp)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/#post-15142385)
 * Hi [@roberton1973](https://wordpress.org/support/users/roberton1973/)
 * Sorry, I’m a bit confused with the logic that you want to implement to the Member
   Directory.
 * Do you want to filter the Member Directory by the “gender_search” selected from
   the Profile Form? And then when the “gender_search” is empty or no selection,
   you want to display the Gender filter to display “Male” and “Female”?
 * Regards,

Viewing 15 replies - 1 through 15 (of 32 total)

1 [2](https://wordpress.org/support/topic/relationship-between-custom-fields/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/relationship-between-custom-fields/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/relationship-between-custom-fields/page/2/?output_format=md)

The topic ‘Relationship between custom fields’ is closed to new replies.

 * ![](https://ps.w.org/ultimate-member/assets/icon-256x256.png?rev=3160947)
 * [Ultimate Member – User Profile, Registration, Login, Member Directory, Content Restriction & Membership Plugin](https://wordpress.org/plugins/ultimate-member/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ultimate-member/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ultimate-member/)
 * [Active Topics](https://wordpress.org/support/plugin/ultimate-member/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ultimate-member/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ultimate-member/reviews/)

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)

 * 32 replies
 * 3 participants
 * Last reply from: [Champ Camba](https://wordpress.org/support/users/champsupertramp/)
 * Last activity: [4 years, 5 months ago](https://wordpress.org/support/topic/relationship-between-custom-fields/page/3/#post-15239925)
 * Status: resolved