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:
- Grab all the posts attached to any of the
Models
child terms - 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. - 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
- Costumes
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!