0

Up until I just discovered, my taxonomy-company.php theme file was successfully generating an indented list of each "company" taxonomy term's corporate family tree.

That is, company is a hierarchical taxonomy. On the term archive page, we get the company's top-most parent and, if there are any, all its children, displaying the full list in a sidebar.

But it has gone rogue for an unknown reason.

I'll omit the later tree-display function from the code below because, at the moment, I speculate the problem is higher up.

  /*************************************************************/
  /*                                                           */
  /*          Get company's corporate family tree              */
  /*                                                           */
  /*************************************************************/

  // $organisation = 'groupm'
  $taxonomy_name = 'company';

  /**
   * Get top level term, cf. https://wordpress.stackexchange.com/a/119268/39300
   */
  function get_top_level_term($term,$taxonomy){
      if($term->parent==0) return $term;
      $parent = get_term( $term->parent,$taxonomy);
      return get_top_level_term( $parent , $taxonomy );
  }
  $topmost = get_top_level_term($organisation,$taxonomy_name);

  $term_id = $topmost->term_id;
  /* Works ok to here */


  /**
  * Get the family
  */
  $args = array(
      'parent' => $topmost->term_id,
  );
  $family      = get_terms($taxonomy_name, $args);
  // print_r($family);            /* This has gone rogue */
  $family_size = count($family);

In the example of accessing the term page for company "GroupM" (slug groupm), the function get_top_level_term() here successfully fetches the term of its parent "WPP" (slug wpp) and we place the latter's ID (7436) in $term_id.

So far, so good. However, when I feed that $term_id to get_terms() as parent parameter, attempting to get all terms beneath WPP, the ensuing $family is now a blank array (Array()). And $family_size, therefore, is 0. This is inaccurate - WPP has many company terms underneath it.

That wasn't happening before. And it now means that the subsequent code to show the corporate tree won't work. Previously, it was fully working with this code. This unexpected failure is really problematic.

I have certainly made changes around my site, though not to the sidebar partial template file which does this. In fact, I have just restored from backup - the sidebar file, the taxonomy-company.php file, another partial file it calls, and a file containing some functions specific to this taxonomy. But there is no change.

I am wondering if I have introduced a filter on get_terms anywhere, or if that's even a thing.

I have run a full trace of calls and find this segment repeating, but it doesn't shed any light for me...

/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'get_term' (length=8)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'get_company' (length=11)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'wp_parse_str' (length=12)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'get_terms_defaults' (length=18)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'parse_term_query' (length=16)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'pre_get_terms' (length=13)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'get_terms_args' (length=14)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'pre_option_company_children' (length=27)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'alloptions' (length=10)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'option_company_children' (length=23)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'get_terms_orderby' (length=17)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'list_terms_exclusions' (length=21)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'sanitize_key' (length=12)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'get_meta_sql' (length=12)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'get_terms_fields' (length=16)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'terms_clauses' (length=13)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'query' (length=5)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'pre_option_db_version' (length=21)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'alloptions' (length=10)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'option_db_version' (length=17)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'sanitize_key' (length=12)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'query' (length=5)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'pre_option_company_children' (length=27)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'alloptions' (length=10)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'option_company_children' (length=23)
/home/mysite/public_html/wp-content/themes/bs4boiler-platform/partials/page-blocks/org/org_sidebar.php(123) : runtime-created function:1:string 'query' (length=5)

There is no error, warning or notice at the affected part. So I don't know how else to find out what has broken this.

How can I fix it?

Update 1:

I note that:

a) My site seems to now be 4.9.8 (I don't remember doing that).

b) According to Codex for get_terms():

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

$terms = get_terms( array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
) );

So, I tried updating my $args specification to the new formulation. But that doesn't make the difference. A difference, with either the old or new method, becomes evident by adding 'hide_empty' => false to either - which reveals all the company terms to have 0 counts!

enter image description here

This is confirmed by doing:

  $myterm = get_term( 7436, 'company' );
  echo 'count: '. $myterm->count;

But these new counts are not true. There are many posts with company terms "groupm", "wpp" and others in the family.

This may be a reason the thing has stopped working.

Perhaps the focus should turn to - how do I fix zeroed post counts for company taxonomy terms? (Or is it still possible that something I've done elsewhere in another file is causing this?)

Update 2:

Unfortunately, wp_update_term_count_now(7436, 'company'); does not accurately update the "wpp" (7436) count beyond 0.

0

Prior to 4.5.0, the first parameter of get_terms() was a taxonomy or list of taxonomies: Since 4.5.0, taxonomies should be passed via the taxonomy argument in the $args array:

So, the lines

 /**
  * Get the family
  */
  $args = array(
      'parent' => $topmost->term_id,
  );
  $family      = get_terms($taxonomy_name, $args);  

should be

 /**
  * Get the family
  */
  $args = array(
      'taxonomy' => taxonomy_name,
      'parent' => $topmost->term_id,
  );
  $family      = get_terms($args);

See Documentation.

I hope this may help.

  • Please see the update I edited just before you responded. I discovered the same thing - but I'm not sure this is the reason. – Robert Andrews Apr 14 at 15:10
  • wp_update_term_count_now( ) may fix the issue. Documentation. And this discussion may also help. – Qaisar Feroz Apr 14 at 15:33
  • As per Update 2: Unfortunately, wp_update_term_count_now(7436, 'company'); does not accurately update the "wpp" (7436) count beyond 0. – Robert Andrews Apr 14 at 15:35
  • wp_update_term_count_now(7436, 'company'); should be wp_update_term_count_now(array(7436), 'company'); – Qaisar Feroz Apr 14 at 15:37
  • Yes, getting IDs of all company terms in to an array, then using that array in wp_update_term_count_now, fixes term post counts, and turns this functionality back on. – Robert Andrews Apr 14 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.