In hook_views_query_alter()
, I am trying to add a join to the user role of the author of the node and filter basing on the role.
$query->add_relationship('user_role_join', new views_join('users_roles', 'uid', 'users', 'uid','INNER'), 'node');
$query->add_where(1,'user_role_join.rid','4' ,'=');
It is something like this, but I am missing something. When I run this query the result is always empty. If I create the condition using Views UI, I do get the correct and expected result. So the data is okay, but my code is the problem.
How do I setup this join correctly in hook_views_query_alter()
?
Also the following code doesn't work.
$join = new views_join();
$join->table = 'node';
$join->field = 'uid';
$join->left_table = 'users_roles';
$join->left_field = 'uid';
$join->type = 'INNER';
$join->extra = array(
array(
'field' => 'bundle',
'value' => 'node',
)
);
$query->add_relationship('user_node_role', $join, 'node');
$query->add_where(1,'user_node_role.rid','4' ,'=');