Is it possible to use a variable in a layer filter? For example, I'd like to have:
"STATE" = @current_state to only display the features within a certain state. Trying to do this gives me an SQL syntax error.
Variables are used in QGIS expressions and can not be used for the feature filter functionality of the layer.
Please also see the answer to QGIS query syntax differs between attribute table and feature filter for detailed information about the difference.
What you could do instead for visualisation purpose is to setup a rule based renderer or use data defined symbology to make use of QGIS expressions with full variable support.
Using QGIS version 3.34, I used the function eval() to "convert" the variable to a string so it can be processed as a query.
In my case I wanted to be able to change the field name used in a query for a rule based renderer, because not always the structure of the data that I was using was the same.
So for a query like:
TIPO in ('22400000','22440000') where I could have the field named LAYER or CODICE, I defined a project variable:
@project_layer_field set as TIPO, and used in the filter
eval( @project_layer_field ) in ('22400000','22440000') and it worked as expected.