the following is my views `$query` output.

    [where] => Array
            (
                [0] => Array
                    (
                        [clauses] => Array
                            (
                                [0] => node.type in ('%s')
                                [1] => node.status <> 0
                                [2] => node.promote <> 0
                                [3] => node.sticky <> 0
                            )
    
                        [args] => Array
                            (
                                [0] => test
                            )
    
                        [type] => AND
                    )
    
            )
    
        [having] => Array
            (
            )
    
        [group_operator] => AND

I want to override it by using `hook_views_query_alter()`. The default operators are all AND, but i want is


    [0] => node.type in ('%s') AND  [1] => node.status <> 0 AND  [2] => node.promote <> 0  OR [3] => node.sticky <> 0

How do I make the last operator to use OR instead.

the following is my code. but it can't work. what's wrong with it?

function mymodule_views_query_alter(&views,&$query) {
  unset($query->where[0]['clauses'][2]);
  unset($query->where[0]['clauses'][3]);

  $query->where[1] = array(
    'clauses' => array(
      0 => 'node.promote <> 0',
      1 => 'node.sticky <> 0',
    ),
    'type' => 'OR',
  );
}
  unset($query->where[0]['clauses'][2]);
  unset($query->where[0]['clauses'][3]);

those lines can work.

Comments

Raf’s picture

I've broken my head on that once, too. It's ALOT simpler than what you'd expect. It's actually so simple you don't see it (which was at least the case with me).

You got these two, right?

[2] => node.promote <> 0
[3] => node.sticky <> 0

Overwrite [2] to be the following:
[2] => (node.promote <> 0 OR node.sticky <> 0)
and do an unset on [3]

That way, the query builder will add the OR as if it were one single condition. The SQL server will handle that OR as an OR, and you'll have the results you want.

enjoylife-2’s picture

ah. your answer works like a charm. you're my hero! many thanks.