How do I check to see if a specific child term has shared posts with another set of child terms within the same taxonomy?

I am working on a stock photography site that uses Easy Digital Downloads. If you’re unfamiliar, it has a Custom Post Type called Download and a Custom Taxonomy called Download Category.

Within that taxonomy, I have a few high-level terms such as Scene, Costume, Model, etc.

On my home page, I have a list of all the Costume child terms (Day Dresses, Ball Gowns, etc.). When a user clicks on one of those terms, they are taken to my taxonomy-download_category.php template, where they can see a grid of all the images in that term. So if they click Ball Gowns on the home page, they will see all the downloads that are attached to the Ball Gowns child term.

I’m trying to add a feature at the top of my taxonomy-download_category.php file that will display a list of models that can be found in whatever Costume child term you are viewing. Using the above example, if you landed on the Ball Gowns page, you’d see a list at the top of the 5 models (out of 9) that have downloads in the Ball Gowns category.

The problem I’m running into is Models is not a Custom Post Type, but rather a sibling term to Costumes under the Download Category taxonomy.

The only potential solution I’ve thought of so far is to:

  1. Grab all the posts attached to any of the Models child terms
  2. Loop through each of those posts and see which ones are also categorized in the current Costumes child term the user is viewing, building an array of term_ids for each.
  3. Use the array I built to output my list.

The issue with this is:

a) It seems like a lot of logic for something that seems like it should simpler.

b) My current code does not work. The filtered_models variable returns as an empty array.

“`

  // Get all models
  $models = get_term_children(204, 'download_category');
  $filtered_models = [];

  // Get ready to query all posts under the Models term
  $args = array(
     'posts_per_page' => -1,
     'orderby' => 'ASC',
     'post_type' => 'download',
     'download_category' => 'models',
     'post_status' => 'publish'
  );

  // Query the posts
  $all_models_posts = new WP_Query( $args );


  if ( $all_models_posts->have_posts() ) {
    while (have_posts()){
      the_post();

      // See which model(s) this post is attached to
      $model_term_args = array(
        'taxonomy'  => 'download_category',
        'parent'    => 204
      );

      $matched_models = wp_get_post_terms( $model_term_args );

      foreach($matched_models as $matched_model) {
        if(has_term('download_category', $term)){
          $filtered_models[] = $matched_model;
        }   
      }
    }
  } 

  // Reset the query
  wp_reset_query();
?>

“`

I’ve searched the Codex for hours looking for a function that may be able to list child terms that have shared posts between another child term, but my searches have come up short. Does such a function exist?

Thank you in advance for your help!

Read more here: How do I check to see if a specific child term has shared posts with another set of child terms within the same taxonomy?

Leave a Reply

Your email address will not be published. Required fields are marked *