I need help creating a view that only returns taxonomy terms NOT assigned to the current post.
I have created a Taxonomy View that lists all the taxonomy terms belonging to a post. All of the taxonomy titles get displayed in a bootstrap grid.
Now I would like to create a 2nd taxonomy view that shows all the taxonomy terms that are NOT assigned to the current post. I would like to display all the unassigned taxonomy titles in a bootstrap grid. I may need to show up to 20 taxonomy titles per post for this scenario. What is the best way to do this?
The UI lets you set up a taxonomy query to include certain terms (e.g. those of the current post) but not to exclude them, although the underlying query made with get_terms does allow that.
So you can use the Views API to modify the query arguments and flip the include condition to become an exclude condition.
So make a taxonomy View that will include the terms you don't want (essentially the same as your first View), and add the following PHP snippet at Toolset > Settings > Custom Code:
add_filter( 'wpv_filter_taxonomy_query', 'ts_exclude_terms', 101, 3 );
function ts_exclude_terms( $query_args, $view_settings, $view_id ){
if ( 87 == $view_id ) // Edit as required
{
// flip include to exclude
$query_args['exclude'] = $query_args['include'];
unset( $query_args['include'] );
}
return $query_args;
}