Home › Forums › General Issues › Taxonomy field on taxonomy term? › Reply To: Taxonomy field on taxonomy term?
Yes, I think that was a similar logic that I had been using…
I had concluded that this method may be slower than desirable. I have know decided that I’ll just have to lumnp it, it doesn’t take light years.
However, the big switch I have made since first trying this method is this – whilse I initially added an ACF Select field as the additional, cosntraining field on the company term, now I am using an actual Taxonomy field. That is, I registered a new taxonomy, Organisation Type (orgtypetax), and have added that as an ACF field. For good measure, I ensured the taxonomy regisgtration code also attaches to the “company” object – though I suspect this is wishful thinking and actually may not be possible.
For reference…
// GET ALL ORGANISATIONS
$organisations = get_terms(
array(
'taxonomy' => "company",
)
);
// NARROW ONLY SPECIFIC ORG TYPE
// Initialise array we will use to feed the WP_Query
$orgs_filtered = array();
// Slug of the orgtypetax taxonomy
$target_type = "agency";
// Check every organisation's orgtypetax for $targettype - if found, add to array
foreach ($organisations as $organisation) {
// Construct prefixed field ref for this organisation
$org_id_prefixed = 'company_'. $organisation->term_id;
// Get ID organisation's orgtypetax taxonomy term
$orgtypetax_term_id = get_field( 'orgtypetax', $org_id_prefixed );
// Now use ID to get the actual object
$orgtypetax_term_obj = get_term( $orgtypetax_term_id );
// If the found term matches the target term, add to our array
if ( $orgtypetax_term_obj->slug == $target_type ) {
array_push($orgs_filtered, $organisation->term_id);
}
}
// Query posts
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
// pagination
'nopaging' => false,
'posts_per_page' => 50,
'paged' => $paged,
// posts
'post_type' => 'article',
// order
'orderby' => 'date',
'order' => 'DESC',
// taxonomy
'tax_query' => array(
array(
'taxonomy' => 'format',
'field' => 'slug',
'terms' => 'viewpoint'
),
// Only organisations filtered above
array(
'taxonomy' => 'company',
'field' => 'id',
'terms' => $orgs_filtered
),
),
);
// the query
$this_query = new WP_Query($args);
if ( $this_query->have_posts() ) :
set_query_var('my_query', $this_query );
set_query_var('name', ucfirst($target_type).' Views' );
set_query_var('count', $this_query->found_posts);
set_query_var('columns', 'col-md-6 col-lg-6 mb-4');
// Template part
get_template_part('partials/card', 'medialist');
wp_reset_postdata(); ?>
<?php else : ?>
Nothing here.
<?php endif;
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.