Skip Navigation

[Resolved] Creating additional string custom taxonomy ID

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by Vane 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1371311

Hi,

I'd like to use in 3rd party plugins several custom taxonomy values.
( it was discussed earlier in: https://toolset.com/forums/topic/how-to-get-a-cutom-taxonomy-id/ )

I understood that Toolset uses WP's default taxonomy identification method, therefore I don't have such taxonomy ID like Toolset's custom fields has, like "wpcf-custom-field-1", but it is an integer.
My idea is can we define fairly easily such a second, string type taxonomy ID in Toolset?
So my custom taxonomy could be both identified as "51" or "wpct-custom-taxonomy-1", so we shouldn't have to modify default behaviour.
I'd like to pass custom taxonomy values to 3rd party plugin ( as prefilled data ), like data for Books ( Author, Genre, Publish date, Title, etc. ) and that 3rd party plugin can only use string ID but not the integer.
( I have data which can not be used as custom field, they must be custom taxonomy )
The slugs could be used with some prefix like "wpct-"

Any idea or solution is appreciated,
thank you!

#1371447

So it sounds like you want to define a custom field value for each term when that term is created. Is that correct? If so, you'll have to use custom code. The WordPress API offers a hook you can use to trigger your own code when a term is created.

function my_create( $term_id, $tt_id, $taxonomy ){
    // do some stuff
}
add_action( 'create_term', 'my_create', 10, 3 );

In the "do some stuff" section, you can assign a custom field value using the update_term_meta function:
https://developer.wordpress.org/reference/functions/update_term_meta/
Toolset fields begin with the prefix "wpcf-", so if your term field slug in wp-admin is "string-id" then the name in the database is "wpcf-string-id":

update_term_meta( $term_id, 'wpcf-string-id', 'your-custom-string-' . $term_id);
#1371481

I'm not sure we are speaking about the same.
I wanted to say I want to add a string ID for the custom taxonomy itself and not the distinct values.
Let's say I have a custom post type (CPT): Company.
I have an instance of this CPT Company, let's say AT&T,
which has a custom taxonomy (CT; "Business Sector") value: "Telecommunications".
This time this CT value stored as integer ("51"), but my 3rd party plugin can not handle this integer value, but it can handle Toolset custom field (CF) values, so if I add the string ID of the CF to the 3rd party plugin's appropriate field, it can handle the related values properly.
So I'd like to pass value "Telecommunications" referring to such a string ID like "wpct-business-sector", like it works with CFs with IDs like "wpcf-custom-field-1"
And I'd like to do similarly with hundreds of Companies (CPT) of dozens of Business Sectors (CT).

#1371535

Right, I think we're on the same page. You want to create a custom field for each term in a custom taxonomy. The name of that custom field should be something like wpcf-custom-field-1, so that a 3rd-party tool can access this data. The value of that custom field should be the same as the term title.

The code I provided before would help you accomplish this for all new terms created in this custom taxonomy. It would be automated. However, it would not add this custom field value to existing terms. You would need additional custom code that falls outside the scope of support we provide here to automate that process for existing terms.

function my_create( $term_id, $tt_id, $taxonomy ){
    if( $taxonomy !== 'your-custom-taxonomy-slug' )
      return;
    $newTermObject = get_term_by( 'id', $term_id, 'your-custom-taxonomy-slug' );
    $newTermName = $newTermObject->name;
    update_term_meta( $term_id, 'wpcf-custom-field-1' , $newTermName );
}
add_action( 'create_term', 'my_create', 10, 3 );

Replace your-custom-taxonomy-slug with the slug of your custom taxonomy. Replace custom-field-1 with the slug of the field used to send this data to your 3rd party plugin. Now create a new term in this taxonomy. Automatically there is a custom field called wpcf-custom-field-1 on the new term with the value equal to the term name.

#1375421

Thank you,
I try a workaround, but appreciate your help!