Should I call a function to query the database in a custom content type template? I am calling the following function inside a template for a custom content type, but my senior developer suggested that calling a function inside a template file is not Drupal standard. 

function some_function($AA_tid = 0) {
  $dis_pt = db_select('some_tble', 'v')
    -&gt;fields('v', array('title'))
    -&gt;condition('some_fld', $AA_tid)
    -&gt;distinct();
  $dis_pt-&gt;join('another_tble', 'ss', 'v.title = ss.type');
  $dis_pt-&gt;fields('ss', array('name'));
  $result = $dis_pt-&gt;execute()-&gt;fetchAll();
  return $result;
}


Should I call the function which will query the database from a content type template? Generally speaking, is it better to avoid such code in a template file?

Edit: function call is present inside the template &amp; its definition is available in some module file.
I believe that depends on what you're doing with your query. According to this Drupal.org community doc you should "refrain from having complex logic in" .tpl files and "for all the conditional logic and data processing of the output, there is the template.php file". IOW template files are for theming/output manipulation while other functions or logic should go into your custom module. So if your query is part of a theming process, yes it should be in your template file. Otherwise in your custom module. 

AFAIK other developers will not be looking for a non-theming function in a template, which is why I think the senior dev suggested that.