Support » Plugin: LH User Taxonomies » User count after relating user to taxonomy term

  • Resolved parakeet



    I have been attempting to relate Users to their company name, in my “firm” taxonomy.
    This needs to happen when I run the WPAllImport plugin import, which is how I get my users onboard.

    To my surprise, as a non-developer, I think I’ve learned how to do this. The following code seems to work, at least for the one batch of users (ie. one firm) I have tried this for…

    // Relate user to "firm" taxonomy terms
    function relate_user_to_firm($company, $username){
      // convert company name to slug format
      $company_slug = lowercase_and_strip($company);
      // use slug to find corresponding taxonomy term
      $term = get_term_by('slug', $company_slug, 'firm');
      // get the object id of this term
      $mytermid = $term->term_id;
      // get user's ID
      $user = get_user_by('login', $username);
      $user_id = $user->ID;
      // *** relate user to this term ***
      wp_set_object_terms( $user_id, $mytermid, 'firm' );

    However, whilst the correct term is displayed under the “Firms” right-hand column in my Users list, the Users count on the actual Firms taxonomy index has not caught up, eg. for a company whose users I just imported, it shows 0.

    Any idea what’s going on there?

Viewing 1 replies (of 1 total)
  • parakeet


    wp_update_term_count( $mytermid, 'firm' ); and wp_update_term_count_now( $mytermid, 'firm' ); do not seem to increment the Users count…

    But adding the $do_deferred = true flag, to “flush the deferred term counts too”, seems to update the count correctly.

    I’m in uncharted territory here… 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘User count after relating user to taxonomy term’ is closed to new replies.