Skip Navigation

[Resolved] adding taxonomy programatically

This support ticket is created 6 years, 12 months 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by susanB-3 6 years, 12 months ago.

Assisted by: Minesh.

Author
Posts
#518902

I have a two-part question.

First,
I set up a CRED post form to create a type "parent." I wanted to create, at the time of submission, a taxonomy term "chaperone" (already created in toolset) using the parent name (title).

This is my code:

add_action( 'cred_save_data_474', 'save_data_for_form_with_id_474', 10, 2 );
function save_data_for_form_with_id_474 ($post_id, $form_data) {
        $my_taxonomy = 'chaperone';
		$post = get_post($post_id);  
        $term_name = $post->post_title;
        wp_set_post_terms($post_id, $term_name, $my_taxonomy);
}

It was working fine, then it just stopped. The only thing that changed was I updated WooCommerce, but this is not a commerce form.

Second,
Is there a way to identify the form other than by form #? This is a multisite installation and of course when the forms are imported into the new site, the form numbers change.

In advance, thank you.

#518915

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please confirm is 'chaperone' is your taxonomy slug or taxonomy term slug?

You can attach the taxonomy terms to newly created post using CRED using WordPress function wp_set_object_terms - check following example:
=> https://toolset.com/forums/topic/assign-parent-category-to-child-post/#post-438559

Do you want to create a new taxonomy term and assign to post while creating new CRED post?

Second,
Is there a way to identify the form other than by form #? This is a multisite installation and of course when the forms are imported into the new site, the form numbers change.
===> No - CRED form ID is the unique thing using which you can identify the CRED form.

#518982
Clipboard02.jpg

Thanks for your reply.

I have attached a screenshot of my "Edit Taxonomy" page, and "chaperone" is the taxonomy slug. It is associated with both the post types "Parents" and "Events."

What I am doing is creating a list of "Chaperones" based on every "Parent" that is entered into the database, so it will automatically be available for selection for events later.

Is there a way, then, instead of using the ID, to test if the post being created is of type "Parent" (actually "family-head" is the slug)?

I changed wp_set_post_terms to:

wp_set_object_terms ($post_id, $term_name, $my_taxonomy);

But it didn't seem to resolve the issue.

#519029

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As I understand - for instance lets say you have created parent post with the title as "this-is-parent", so you want to add this post title as taxonomy TERM same as parent post title "this-is-parent" so that you can use "this-is-parent" as taxonomy term for future selection - correct?

#519105

Yes I believe you are understanding the problem.

#519264

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can I have problem URL and tell me which post title you want to add as taxonomy term.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#519386

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've added following code to your current theme's functions.php file:

add_action( 'cred_save_data_127', 'save_data_for_form_with_id_127', 10, 2 );
function save_data_for_form_with_id_127($post_id, $form_data) {
         $my_taxonomy = 'chaperone';
        $post = get_post($post_id);  
        $term_name = $post->post_title;
		$term_slug = sanitize_title($post->post_title);

/// To add taxonomy term as parent post title to taxonomy "chaperone"
$term = wp_insert_term(
  $term_name, // the term 
  $my_taxonomy, // the taxonomy
  array(
    'description'=> '',
    'slug' => $term_slug,
    'parent'=> 0
  )
);
         // to attach created taxonomy term to created post  
        wp_set_object_terms($post_id, $term['term_id'], $my_taxonomy);
}

I made a test by adding "Toolset Test" as parent post title and I can see now taxonomy term "Toolset Test" added to taxonomy "chaperone" same as parent post title.

Could you please confirm. Please feel free to get in touch with me with your queries regarding this issue, if any.

#519871

Sorry for my delay in replying.

Thank you for your assistance. This code does work!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.