I am working on a small matrimonial application. I've installed the Profile 2 module and added a few fields like 'interested_in', 'gender', 'partner preference', etc to the users of my site.

I need to create a view that will list all profiles with same gender as logged in user's 'interested in' field.

How can this be achieved?

1 Answer 1


Solution that worked for me:

I used MyModule_views_query_alter hook and added a join to the field table and the condition that matches the choice of logged in user and gender of users in db.

if ($view->name == 'profile_list') {
        $logged_in = $GLOBALS['user']->uid;
        $pr = profile2_load_by_user($logged_in,'main');

        $join = new views_join();
        $join->table = 'field_data_field_gender';
        $join->field = 'entity_id';
        $join->left_table = 'profile';
        $join->left_field = 'pid';
        $join->type = 'inner';

        $query->add_relationship('field_data_field_gender', $join, 'profile');

        $view->query->add_where(0, db_and()->condition('field_data_field_gender.field_gender_value', $pr->field_looking_for['und']['0']['value'], '='));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.