The reasons your pre_get_posts
filter does not work on the widget are
-
the condition
!$wp_query->is_main_query()
makes the filter handle only the main query and nothing else -
Your special parameter
myattr
is “self populating” only on the main query but it is not propagating automatically to other queries.
To make it work on the widget, the first thing you will need to do is remove the conditions that bails out if it is not the mail query (your attribute should not be set in other queries so should not make a difference).
Then it gets complicated. you can get the attribute from the mail query by changing get_query_var('myattr')
into
global $wp_the_query;
$wp_the_query->get('myattr','')
but in this case it will affect all queries on the page, which might be ok depending on what you have there
The other option is to find a way to change the query of the widgets itself and inject your attribute, which seems pointless as the actuall change you do to the query is simple, but might be worth it if you want to maintain the changes in only one place