Skip Navigation

[Resolved] I need to only display a loop view based on taxonomies for the parent post

This thread is resolved. Here is a description of the problem and solution.

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 support ticket is created 7 years, 2 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.

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 garyF-3 7 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#563394

Where did you add the filter code? I can't see it in functions.php - I'm looking at Appearance > Editor > Digital Pro > functions.php

#563401

Hi, I have placed the code in the following:

Dashboard > Genesis > Extender Custom > Functions tab

#564004

Ah, I see the issue. I needed to update the query to only search for credits that are children of the current Website. I've updated the code to fix that. Edit - see updated code below.

#564796

Hi Christian, apologies for the late response, we had a national holiday yesterday. The code works perfectly when there is any child content - so brilliant.

The only problem is that if a website has no child content at all it shows all the tabs that exist - you can see this here hidden link

Sorry to bother you again on this

#564838

No problem, let me know if you catch any other edge cases and I can update this again.

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

Hi Christian, this works perfectly. I think you went above and beyond the normal response. This has solved my problem and we are so thrilled with the result. Thank you for all your help, very good of you.

the best support ever 🙂