How can I filter users within a user-configurable range in Views? For example, consider a dating website.  Each user has an Age stored in their profile.  They also have a Max age and Min age, which are the maximum and minimum ages of people they hope to receive interest from (in other words, users with ages outside the range Min age - Max age should not be allowed to contact that user).

How can I build a page in Views that filters the results for the current user to show other users that both meet the current user's age range and for which the current user meets their age ranges?

In other words, I need to somehow filter the results like this:


Only show users that have ages within the range of the current user's Max age and Min age (example: current user is 20 and wants to see users from 18-25; filter to show users from 18-25) 
Only show users for which the current user has an age that falls within the range of their Max age and Min age (example: current user is 20 and wants to see users from 18-25; filter to show users who want to receive messages from someone age 20)


Is this something I can somehow do with contextual filters, or do I need to write my own custom filter?
Install sandbox project Views arguments in filters
Add four contextual filters to your user view:

User:Uid, hide view when filter not available, Exclude (you will send in the user's own uid and exclude him from the result as you do not want him to be matched against himself)

Global:Null, hide view when filter not available, and give it an administrative title of 'current age'. (You will send it the current user's age as a param).

Global:Null, hide view when filter not available, and give it an administrative title of 'min age'. (You will send it the current user's min age preference as a param).

Global:Null, hide view when filter not available, and give it an administrative title of 'max age'. (You will send it the current user's max age preference as a param).





Then add normal filters as follows:




That should take care of the view, and you now just have to construct a link to add the parameters manually, or override a view hook to do so. Possible candidate would be hook_views_pre_view where you have direct access to args param, but on other hooks it should be available on the $view object.
Something like this code will work, and you can just add your own defensive programming and substitute where appropriate.
function MYMODULE_views_pre_view(&amp;$view, &amp;$display_id, &amp;$args) {
  if ($view-&gt;name == &quot;compatible&quot; &amp;&amp;  $display_id == &quot;page&quot; &amp;&amp; user_is_logged_in() &amp;&amp; count($args) != 4) {
    global $user;
    $account  = user_load($user-&gt;uid);
    $age = field_get_items(&quot;user&quot;, $account, &quot;field_age&quot;);
    $min_age = field_get_items(&quot;user&quot;, $account, &quot;field_min_age&quot;);
    $max_age = field_get_items(&quot;user&quot;, $account, &quot;field_max_age&quot;);
    $args = array(
      $account-&gt;uid,
      $age[0]['value'],
      $min_age[0]['value'],
      $max_age[0]['value']
    );
  }
}