Creating filter seems to work but doesn't "stick" So I want to create a view filter programatically using hook_update in the .install file. I feel like I have it going, but the changes will not show up in the UI. Here is what I am doing, and the results:

This view already contains 2 filters. I used this code to view those filters:

$filters = $view-&gt;display_handler-&gt;get_option('filters');
var_dump($filters);


and it was clear that they were there. Then I went into the UI and created the new filter using the GUI tools there. Then I ran the above code again, and it showed me all 3 filters (the 2 previous ones, plus my new one). So far so good.

So now I backtracked the database to the initial state, so I only had 2 filters again. Then inside my install file I ran this code:

$name = "soccer_scores";
$view_display = 'default';
$view = views_get_view($name);
$view-&gt;init_display($view_display);
$view-&gt;preview = TRUE;
$view-&gt;is_cacheable = FALSE;


$filters = $view-&gt;display_handler-&gt;get_option('filters');

$filters['field_is_livescore_value'] = array(
'id' =&gt; 'field_is_livescore_value',
'table' =&gt; 'field_data_field_is_livescore',
... a bunch of other array data

$view-&gt;pre_execute();
$output = $view-&gt;display_handler-&gt;preview();
$view-&gt;execute();
$view-&gt;post_execute();
var_dump($filters);


So after the install file runs, when that last var_dump runs, it prints out the $filters array and it looks exactly like when I created the new filter using the GUI. However, when I reload the admin area for that view, no new filter appears, it just shows the existing 2 filters, but not the new one.

So I feel there is some step I am missing to make the change "stick". I can find so few docs on this, can anyone help?
To modify the value of a view programatically you don't need to actually render the view like you do with this part:

$view-&gt;pre_execute();
$output = $view-&gt;display_handler-&gt;preview();
$view-&gt;execute();
$view-&gt;post_execute();


They are only functions involved in rendering the view output.

The part you are missing is saving the view.

You are adding things to it but because you don't save it they don't end up in the database.

You will need to use the view::save() function like this:

// Load the view.
$view = views_get_view('VIEW_NAME');
// Modify the view here.
...
// Save the view.
$view-&gt;save();


[EDIT]

As for actually adding the filter. I recommend using the view::add_item() function instead of just manually adding to the view object because it does some extra work with the handler that isn't being done by your current code.

Some examples of using add_item() are https://drupal.stackexchange.com/a/134872/10729 and https://drupal.stackexchange.com/a/70649/10729