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 $clearbit_company_name$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 $clearbit_company_name$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
$clearbit_company_name$company
- Strip spaces and make
$clearbit_company_name$company
lowercase using my convert_company_name()
function.
- Identify taxonomy "firms".
- Step through "firms" taxonomy to find a term whose slug matches our processed
$clearbit_company_name$company
string (eg. "widgetsinc" for User's processed $clearbit_company_name$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:
I notice wp set object terms
"relates an object (post, link etc) to a term and taxonomy type". And a WordPress user is also to be considered an object,right?
So, if that expects expression like wp_set_object_terms( $object_id, $terms, $taxonomy)
, wherein $object_id
is the object being related to a term, I need to find the object ID of the user in question, right?
I have been mocking up this code, to try to visualise. I haven't run it yet, and there are some question marks over how I would run it through WPAllImport:
// convert company name to slug format
$company_slug = company_name($clearbit_company_name[1]$company[1]);
// use slug to find corresponding taxonomy term
$term = get_term_by( "slug", $company_slug, "firm" );
// get the object id of this term
$term_id = $term->term_id;
// get user's ID
$user = get_user_by('login', $username[1]);
$user_id = $user->ID;
// set term against this user
wp_set_object_terms( $user_id, $term_id, "firm" )