Building an Advanced Search (text, tags, category, custom fields) – Getting the wrong SQL query

SOLUTION:

I will post my solution and explain what was the problem and I hope that this post can help someone in the future.

As we talk in the small comments the problem, was that I was sending a non existing taxonomy by error and this was causing the AND (0=1).

To fix the whole problem I created a add_rewrite_rule for each case when I have parameter or not like the following example:

add_rewrite_rule(
   '^cerca/comarca/([a-zA-Z0-9-+]+)/?',
   'index.php?s=&category_name=$matches[1]',
   'top'
);

 add_rewrite_rule(
   '^cerca/municipi/([a-zA-Z0-9-+]+)/?',
   'index.php?s=&municipi=$matches[1]',
   'top'
);
   add_rewrite_rule(
   '^cerca/provincia/([a-zA-Z0-9-+]+)/?',
   'index.php?s=&provincia=$matches[1]',
   'top'
);
add_rewrite_rule(
   '^cerca/provincia/([a-zA-Z0-9-+]+)/comarca/([a-zA-Z0-9-+]+)/municipi/([a-zA-Z0-9-+]+)/?',
   'index.php?s=&provincia=$matches[1]&category_name=$matches[2]&municipi=$matches[3]',
   'top'
);

….

Thanks to @janh2 for all the helpful comments.

error code: 523