Skip Navigation

[Resolved] Automatically save taxonomy on CRED form using API

This support ticket is created 8 years 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Peter 8 years ago.

Assisted by: Luo Yang.

Author
Posts
#381098

I am trying to get my CRED form to automatically select and save one taxonomy tag on submit.

I have been through suggested documentation but I just can't get it to work.

- I have setup the taxonomy called 'group'
- Inside this 'group' the actual tag I want to save has an id of '10'
and here is the code I have:

/**
 * Automatically Save Form Taxonomy.
 */
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==1129)
    {
        if (isset($_POST['group']))
        {
	    // define taxonomy ID
            $tag = array( 10 );
	   // add it to saved post taxonomy
	   wp_set_post_terms( $post_id, $tag, $taxonomy);
        }
    }
}

any help with this save data hook will be greatly appreciated.

#381142

Dear Peter,

The wordpress function wp_set_post_terms() will only take effect on wordpress native post type, I suggest you use function wp_set_object_terms() instead of it, for example:

/**
 * Automatically Save Form Taxonomy.
 */
add_action('cred_save_data', 'set_default_group_func',10,2);
function set_default_group_func($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==1129)
    {
// if the user isn't select any group term
        if (!isset($_POST['group']))
        {
        // define taxonomy ID
            $tag = array( 2 );
       // add it to saved post taxonomy
			wp_set_object_terms( $post_id, $tag, 'group');
        }
    }
}
#382205

Great thank you, your solution worked perfectly.

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