Skip Navigation

[Resolved] View To Only Display Empty Taxonomy Terms

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by Joe 4 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1782749

Joe

Hello,

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?

#1783913

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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;
}

You should just need to edit the View ID.

#1784469

Joe

That worked great and ended a lot of frustration for me. Thank you!