Support

Account

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…

        Add a field for the taxonomy term
        Get all companies
        For each, check the addtional ACF field
        If it matches the one we want to constrain, add its ID to an array
        When done, use that array to feed specific company term IDs to the WP_Query. In other words, narrow things

    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;
    
           ?>