0

I am attempting to import several users, who each should have a connection with a term in a taxonomy named "firm" - but I don't know how to make the connection...

Background:

FYI, I have already enabled WordPress taxonomy support for Users using plugin LH User Taxonomies, and taxonomy "firm" has already been registered. I am importing using plugin WPAllImport.

65 "firms" terms have been created via prior WPAllImport import. Term slugs underwent text processing on inbound company name field $company to strip spaces and convert to lowercase, in order to create a clean term slug.

All this is set up, and the question is not about enabling taxonomy support for Users.

The problem:

I am now importing many Users, and need to connect them to an existing "firm" term (ie. the company they work for). Unfortunately, WPAllImport does not support import to taxonomy terms for Users - but it does support PHP functions executed upon each user import, and actions like pmxi_after_xml_import, which fires after import.

The best way to match seems to be via slug - that is, matching inbound User field $company, after stripping spaces, against a "firm" term with matching unique slug. But how do I do this, and then how do we make the association in the database?

On each individual User import, logic may look something like this:

  • Identify inbound string $company
  • Strip spaces and make $company lowercase using my convert_company_name() function.
  • Identify taxonomy "firms".
  • Step through "firms" taxonomy to find a term whose slug matches our processed $company string (eg. "widgetsinc" for User's processed $company, matching "widgetsinc" term slug)
  • Associate this User with that slug, ie. set the term.
  • Any appropriate fallback.

I'm not sure how any of the code would be written for this.

wp set object terms seems like it might be involved?

Edit (2):

I think I have learned how to do a lot here. This seems to work, to a point...

// 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' );

}

The trouble is, whilst the correct terms show under the "Firms" column on my Users listing page, the Users count against the actual term on the Firms listing page is not updated, it still shows 0. What's going on there?

  • I edited the question to reflect a partially-working solution. – Robert Andrews Jul 17 '17 at 8:27
  • 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. – Robert Andrews Jul 17 '17 at 9:31
0

users are humans (or extensions of humans) that have some level of admin access, taxonomies are for "categorizing" content, humans are not content. In other words - if you feel like you need to associate a taxonomy with users, you are probably doing something wrong, or at least shady.

The only association that there should be between a user and content is whatever setting are required to determine if the user can edit that content, but it sounds like you are trying to get users "categorized" with taxonomies, which is wrong.

  • 1
    "you are probably doing something wrong, or at least shady." I disagree; there are good reasons you may want to categorize users into taxonomies. For example, you might have a feature where authors can be associated with the post categories that they specialize in. – John Ellmore Mar 30 '18 at 16:37
  • @JohnEllmore, In the context of wordpress taxonomies are for content and users are not content, as simple as that. If you need a user to be associated with a category (whatever that means) you use the capabilities APIs or user meta to create it, depending on the exact context – Mark Kaplun Mar 30 '18 at 20:42
  • I'm not understanding this content/not-content distinction you're making. Users have bios, meta fields, etc.; that's all content. WordPress sites can easily have millions of users, and having taxonomies for those would be handy in edge cases. Taxonomies as they are in WP core are mainly intended for post types, but that doesn't mean it's wrong or shady to use them for other database objects (as long as you're aware of the pitfalls). – John Ellmore Mar 30 '18 at 20:59
  • @JohnEllmore, as you said, users has content associated with them. Stephen King has books, you can certainly classify his books with taxonomies. Stephen King the human, has permissions to access the content of his books, those permissions are not content, His password can not be taxonomified. As with all other code you can hack and misuse things, but in the long run it is not healthy to treat things as if they are different then what they are. – Mark Kaplun Mar 31 '18 at 3:32

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.