Passing variables via template.php for search module I'm using search.module. I used dpm(get_defined_vars() ); in my search-results.tpl.php file to see all returned variables, so I can reference a needed variable in some PHP code I'm writing in there. 

However, although the variable is being returned in the teaser/snippet (and is outputting to the page) the only place I'm finding the 'variable' is in the $search_results array which is just a massively long string of html code. How can I parse out a needed variable (I assume in template.php) so that I can reference it and use it for comparisons on search-results.tpl.php?

The content is coming from a content type node called 'resource'. The field in questions is called 'field_type'. Here's the code I'm currently working with in template.php to get other variables:

function themename_preprocess_search_results(&amp;$variables) {
        foreach($variables['results'] as $result) {
                $node = $result['node'];
                if($node-&gt;type == 'resource') {
                      $items = field_get_items('node', $node, 'field_type');
                      $variables['field_type_items'] = $items;
                      $variables['field_type_item'] = $items[0];
                      $field_view = field_view_field('node', $node, 'field_type');
                      $variables['field_type_rendered'] = render($field_view);
                }
        }
}

// function to enable correct variables for search results
function themename_preprocess_search_result(&amp;$variables) {
  $variables['search_results'] = '';
  if (!empty($variables['module'])) {
    $variables['module'] = check_plain($variables['module']);
  }
  /*foreach ($variables['results'] as $result) {
    $variables['search_results'] .= theme('search_result', array('result' =&gt; $result, 'module' =&gt; $variables['module']));
  }*/
  $teaser = node_view($variables['result']['node'], 'teaser');
  $variables['teaser'] = $teaser;
  /*
  $variables['body'] = substr(render($teaser['body']), 0, 200);
  if (strlen($variables['body']) &gt;= 200) {
    $variables['body'] .= "...";
  }
  */
  $variables['snippet'] = drupal_render($teaser);
  $variables['info'] = '';
  $variables['pager'] = theme('pager', array('tags' =&gt; NULL));
  $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
}


Is there something I can add to this to get the field field_type and pass it to my tpl files? 

Or, if that's not the right solution, what is?
Adding fields programatically

Use plain old dpm($variables); in your search-results.tpl.php file to check variables.

And yes, the preprocess function is where to add in extra information, you can use something like:

$variables['custom'] = field_view_field($entity_type, $entity, $field_name)


field_view_field prevents you from having to load the whole node and takes care of languages etc for you.



Adding fields in the UI

Go to the Manage Display tab for the content type in question and scroll down to CUSTOM DISPLAY SETTINGS.

Turn on the Search result checkbox and save, then go to the Search Result sub-tab.

Arrange your fields how you wish them to show in search results - note that fields aren't available to the template file unless they are displayed in the Manage Display tab, this might be the root cause of your problem.