I have multiple node types on my website and a special View listing all the nodes on the site.

I have a taxonomy field which I have applied to some node types but not others. The trouble is any user with editor permissions can add the taxonomy field to any node type which previously did not have it.

My Views needs to list content only of node types which have the taxomony field included. In other words, if a node type does not have field_taxonomy_example included, my View should list NO nodes of that type.

My problem is I can't find a way to dynamically exclude node types - statically setting them is not an option because new types can be added any time.

Please help :-)


I found a programmatic solution after visiting the Views API page.

Using the following code:

function mymodule_views_query_alter(&$view, &$query){
$types = db_select('field_config_instance', 'fci')
        ->fields('fci', array('bundle'))
        ->condition('field_name', 'field_taxonomy_example', 'like')

$query->add_where(0, $query->fields['node_type']['field'], $types);


I was able to insert a dynamic list of node types to test my Views again.

Works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.