Adding an OR condition to an existing query I want to be able to search additional user fields instead of just the standard username. For instance, I would like users to be able to search an attached field with the users' Real Name. The user search query can be found in user_search_execute(). I figured I would use hook_query_alter() to alter the database query generated by this function. 

The problem I am running into is that if I add anything like this:

$arguments = $query-&gt;getArguments();
$query-&gt;condition('n.field_name_value',$arguments[':db_condition_placeholder_0'], 'LIKE');
$query-&gt;leftJoin('field_data_field_name', 'n', 'users.uid = n.entity_id');


Then drupal adds a "AND" to the list of conditions (instead of an OR).

I have even tried adding something like this:

$arguments = $query-&gt;getArguments();
$query-&gt;condition(db_or()-&gt;
  condition('n.field_name_value',$arguments[':db_condition_placeholder_0'], 'LIKE'));
$query-&gt;leftJoin('field_data_field_name', 'n', 'users.uid = n.entity_id');


However, this produces the same results.

thanks!
Adding new conditions to the query won't overwrite the existing ones, you need to reset the existing array of conditions, build up a new one and add it to the query:

// Get the query args and then the search term
$args = $query-&gt;getArguments();
$search = array_shift($args);

// Get a reference to the existing query conditions.
$conditions =&amp; $query-&gt;conditions();

// Reset the condition array. It needs a default #conjunction for which AND is fine
$conditions = array('#conjunction' =&gt; 'AND');

// Build up your condition
$condition = db_or()
  -&gt;condition('users.name', $search, 'LIKE')
  -&gt;condition('n.field_name_value', $search, 'LIKE');

// Add the new OR condition to the query
$query-&gt;condition($condition);

// Join the relevant table
$query-&gt;leftJoin('field_data_field_name', 'n', 'users.uid = n.entity_id');


I've just tested that to work on a local installation.

There is a slight caveat...for users with the 'administer users' permission the default is to also search through users' email addresses. You can cater for this by adding the following lines immediately before $query-&gt;condition($condition);:

if (user_access('administer users')) {
  $condition-&gt;condition('mail', $search, 'LIKE');
}


It might also be wise to test the bundle and entity_type columns in the joined field table in case you ever attach the same field to another entity. This way you won't get any false results:

$join = "users.uid = n.entity_id AND n.entity_type = 'user' AND n.bundle = 'user'";
$query-&gt;leftJoin('field_data_field_name', 'n', $join);


EDIT

As per Berdir's comment, to maintain as much compatibility as possible with other modules that might also alter this query, you probably want to loop through the conditions, find the one(s) that you need to change and do so on an individual basis. 

The above example is pretty rudimentary and works on a 'vanilla' Drupal install, hopefully it'll get you pointed in the right direction.