How do I get all terms matching:
- custom taxonomy "
company
" - only where term meta (ACF field) "
tags
" value matches a given string, "Publishing
" - only if the term has any posts of custom type "
article
" - but those posts must only have a taxonomy "
format
" value of term "viewpoint
" or one of its children
... ?
My current code fulfils 1) and 2) above - but I do not how how to add the additional constraints of 3) "article
" and 4) "format
"...
$args = array(
'meta_query' => array(
array(
'key' => 'tags',
'value' => 'Publishing',
'compare' => 'LIKE'
)
),
'taxonomy' => 'company',
'hide_empty' => false,
'number' => 10,
);
$orgs_matched = get_terms($args);
echo '<ul class="list-group list-unstyled">';
foreach ($orgs_matched as $matching_org) {
echo '<li class="list-group-item d-flex justify-content-between align-items-center">';
echo '<span><img src="https://www.google.com/s2/favicons?domain='.get_field( 'domain', $matching_org ).'" class="mr-2"><a href="' . esc_url( get_term_link( $matching_org ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $matching_org->name ) ) . '">' . $matching_org->name . '</a></span>';
echo '<span class="badge badge-primary badge-pill">'.$matching_org->count.'</span>';
echo '</li>';
}
echo '</ul>';
In my setup, "article
" posts can have associations with:
- "
format
" taxonomy term, with name values including "viewpoint
". - "
company
" taxonomy terms, which themselves have an ACF Checkbox field, "tags
", one of whose values is "Publishing
".
I want to get only the "company
" terms which have a "tags
" value of "Publishing
" - but I am only interested in getting those companies which have associated "article
" posts that also have taxonomy "format
" values of "viewpoint
" or beneath.
For each resulting "company
' term, I would want to count the number of "article
" posts of taxonomy format "viewpoint
" and beneath.
Update:
I'm contemplating the method below. Basically, the standard get_terms
below, which works, and then stepping through the resulting array to filter out any terms which don't have the term in question...
$args_for_short = array(
'taxonomy' => 'company',
// ACF "tag" checkbox field
'meta_query' => array(
array(
'key' => 'tags',
'value' => 'Publishing',
'compare' => 'LIKE'
)
),
'orderby' => 'count',
'order' => 'desc',
'hide_empty' => false,
'number' => 10,
);
// array: actual term objects
$orgs_list_short = get_terms($args_for_short);
// Strip non-Viewpoints out of the terms array
$orgs_list_short_filtered = array();
foreach ($orgs_list_short as $org_short_single) {
// Get all posts of type 'article'
// For each 'article'
// Examine associated "format" terms
// If 'format' term "viewpoint" or child is not present
// Remove this term from the term object array
}
get_terms()
alone.get_terms
any terms which are not associated with the post type and secondary taxonomy term. Seems like it may be a little intensive. Anyone can feel free to weigh in; thanks.