Creating a filter programatically with views_get_view I have a lot of views and I want to add a filter to just one of them. This view is called soccer_news_feed and I want it to check out my custom field: field_data_field_is_score and display if it's not 1.

So in a hook_update_n I have this code:

$view = views_get_view("soccer_news_feed");

$view-&gt;add_item(
  $view-&gt;current_display,
  'filter',
  'field_data_field_is_score',
  'field_is_score_value',
  array('operator' =&gt; '&lt;&gt;','value' =&gt; array('value' =&gt; 1))
);


So I thought maybe this work but I get this error when I run updb:

PHP Fatal error:  Call to a member function get_option() on a non-object in /var/www/public/sites/all/modules/contrib/views/includes/view.inc on line 2450


I'm very lost on this.
The first key to enlightenment is to at error message.  It says the fatal error happened in views/includes/view.in, around line line 2450. I don't know your exact version, but in one of my Drupal 7 installs, here's what's close to line 2450:

  /**
   * Add an item with a handler to the view.
   *
   * These items may be fields, filters, sort criteria, or arguments.
   */
  function add_item($display_id, $type, $table, $field, $options = array(), $id = NULL) {
    $types = views_object_types();
    $this-&gt;set_display($display_id);

    $fields = $this-&gt;display[$display_id]-&gt;handler-&gt;get_option($types[$type]['plural']);


My line numbers don't match in my version, but notice the last line of the excerpt.  Best guess that $this->display[$display_id]->handler ($this is your view) is either NULL, or is not an object.

$this->display is an array of view_display objects; and $this->display($display_id) should either be a view_display, or perhaps, NULL or FALSE.

I'd recommend trying the following to debug this:


First, perhaps it's possible that $view->current_display is not initialized, or isn't what you think it is. Try substituting it with 'default', which usually works with Views. 
If you look at your view's definition and know the machine ID of one of your displays for that view (say, 'page_1'), try substituting that and see if it works.
If you're using an IDE with a source debugger, this is a good time to learn to use it a bit. Views can be pretty opaque as to what it's doing under the hood, and sometimes, seeing what the data actually is in real time can save you a lot of time.


That's as much as I can suggest w/o knowing the exact version of Views you're using, but it may get you started.

Good luck, in any case.