Problem: I have two Views of taxonomy terms, shown as tabs and tab panels, in a tab-based interface that allows users to select a term and show the associated term panel. This tab interface is displayed in a parent post type. In the tab panel of each term I include another View that shows all the child posts associated with that term. If no child posts are found, I do not want to show the tab or tab panel.
Solution: There's not a good way to apply conditional logic to a parent View that tests the output of a child View. Instead, you must apply a taxonomy query filter with the following custom code. This code loops over all the child posts associated with the current parent post, and builds an array of all the associated terms. If a term is not associated with any child, it will not be added. Then apply this as the "include" option in your taxonomy query as shown here:
add_filter( 'wpv_filter_taxonomy_query', 'prefix_only_assigned_tax_query', 10, 3 ); function prefix_only_assigned_tax_query( $tax_query_settings, $view_settings, $view_id ) { global $post; $views = [243391, 243393]; if( in_array( $view_id, $views) ) { // query all credit posts and build an array of used cred-type terms $ids = array(); $args = array( 'post_type' => 'credit', 'meta_query' => array( array( 'key' => '_wpcf_belongs_website_id', 'value' => $post->ID ) ) ); $ps = get_posts( $args ); foreach($ps as $post) { // loop over each post and push its terms into the array $terms = wp_get_post_terms( $post->ID, 'cred-type'); foreach($terms as $term) { if( !isset( $ids[$term->term_id] ) ) { $ids = array_merge($ids, array($term->term_id)); } } } $ids = count($ids) ? $ids : array(9999999); $tax_query_settings["include"] = $ids; } return $tax_query_settings; }
Relevant Documentation: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query
https://codex.wordpress.org/Class_Reference/WP_Query
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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 20 replies, has 2 voices.
Last updated by 7 years, 2 months ago.
Assisted by: Christian Cox.