0

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!

Edit: Here's a visual hierarchy of the taxonomy:

  • Download Category (taxonomy)
    • Costumes
      • Day Dresses
      • Ball Gowns
    • Models
      • Amber
      • Brittany
      • Megan

UPDATE:

I realized why my $filtered_models array was empty after the foreach loop — it's because I was using wp_get_post_terms but wasn't supplying the arguments correctly. After looking at wp_get_post_terms more closely, it doesn't seem to allow for selecting specific child terms, so I decided to change to get_term_children. I'm now getting an error (screenshot: http://files.builtbygood.co/pG3L/5lwBxULw) but I still consider this progress.

Here's the updated code:

```

  // 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 'dem posts!
  $all_models_posts = new WP_Query( $args );


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

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

      var_dump($models);

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

  // Reset that query
  wp_reset_query();
?>

```

Any help at all is greatly appreciated!

  • I understand that you are trying to show other child terms assigned to current listed post. Is this correct? – BlueSuiter Apr 13 '17 at 5:27
  • Not quite. Here's the hierarchy that may make it easier to understand: Ex: I'm on the Ball Gowns page. Let's say for example that out of all the models, Amber and Megan are the only models that appear in photos that are also in the Ball Gowns category. I therefore want to display Amber and Megan at the top of the Ball Gowns page. So, now posts are being displayed, only terms. – J.C. Hiatt Apr 13 '17 at 14:32
  • @AmitChauhan I added an edit to the bottom of my question that contains a visual hierarchy. – J.C. Hiatt Apr 13 '17 at 14:39
0

You're definitely getting an array of what I believe is all of the models, but you should be able to use has_term like you did, but with the $model variable as what to check for. $model should be the ID number for the categories that you're seeing.

And then just use get_cat_name by passing in $model. I'm at a loss if this doesn't work.

// 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 'dem posts!
  $all_models_posts = new WP_Query( $args );


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

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

      foreach($models as $model) {
           if(has_term($model, 'download_category')){
                echo get_cat_name($model);
        }
      }
    }
  }

  // Reset that query
  wp_reset_query();
 ?>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.