Support

Account

Home Forums General Issues Taxonomy field on taxonomy term?

Solving

Taxonomy field on taxonomy term?

  • I may need a way to sub-describe taxonomy terms.

    One of my taxonomies is “company”.
    But now I see the need for a “company type”.

    I have previously implemented this as a Select field on the “company” taxonomy terms. But, when it comes to outputting post type “articles” of eg. “company” type “agency” the overhead of that WP_Query makes the query quite a lot.

    So I am curious about creating another taxonomy for “company type”.

    If I go inside the “company” field group and add the “company type” taxonomy as another field, how can I use this, if at all?

    I have just experimented with eg. $company_type_terms = wp_get_object_terms( $company->term_id, 'company_type' ); where $company is the current object (ie. taxonomy-company.php) – but this didn’t seem to pick up any terms, and I assume wp_get_object_terms wants to look in post objects.

    What avenues are open?

    Thanks.

  • There is nothing easy or simple.

    Taxonomy => company
    Taxonomy => company-type

    Where each company has a selected company type

    To do this you would need to get all of the terms in company using get terms. Then you would need to loop over the entire set and use get_field() to get the company type. You can’t use get_terms() because in WP terms cannot have terms. Using this method you can assemble your own subset of companies that have a specific company type.

    Going back to the beginning I would probably have made company a custom post type with company type being the taxonomy.

  • 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;
    
           ?>
    
  • Just curious to add a couple of things/questions here…

    1. Does the code I have used above match the kind of suggestion you had given the circumstances?

    (To convey a company type, I have weighed up using either 1) a taxonomy, linked to the company term via an ACF field, or 2) just an ACF field itself. My code above uses a taxonomy).

    2. I’m now considering that the “company type” descriptor may actually need to be multiple

    In other words, I may have available multiple source pieces of data with which to describe my companies…

    1. “tags”
    2. “category: sector”
    3. “category: industryGroup”
    4. “category: industry”
    5. “category: subIndustry”
    6. “category: sicCode”
    7. “category: naicsCode”

    I am now considering that, if I choose to use them all, this is clearly too much to fit in to one “company type” as taxonomy.

    So I am now considering going back to baking the company descriptive field/s in to the company term itself as ACF fields, and not using a separate “company type” taxonomy…

    i)

    I had initially believed that doing it that way would make the looping slower.

    But, since I am already currently linking that taxonomy to a term using an ACF field anyway, maybe there is no impact to just pulling that data from term meta (ACF fields on “company”) when it’s open (?).

    ii)

    I will still want to output posts by company type in various ways. I’d value discussing this…

    It seems that using a taxonomy to describe “company type” gives the natural advantage of spinning up template pages (eg. taxonomy-orgtype-agency.php to output posts for all “agency” companies).

    If I move to storing all “company type” fields as ACF fields against “company” terms themselves, how would I go about grouping output of posts per company type?

    To simplify that question – say I have 200 ‘company” terms with a Checkbox “org_tag” value of “agency”. How do you output only those? …

    Because, unlike a taxonomy, I assume there is no relation between entities with the same value? Is there an ACF field that does have this relational database-ability? Leaving aside the Relationship, which seems to want a relationship with a Post.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Taxonomy field on taxonomy term?’ is closed to new replies.