Skip Navigation

[Resolved] Set default category for posts using toolset post forms

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to set default categories using Toolset Forms?

Solution:

Add the code below:

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"]==7220) {
        $category_ids = array(82); // replace your category ID
        wp_set_object_terms( $post_id, $category_ids, 'category');
    }
 
    if ($form_data["id"]==7232) {
        $category_ids = array(83); // replace your category ID
        wp_set_object_terms( $post_id, $category_ids, 'category');
    }   
}

Change the Form IDs and Category IDs with the IDs you have on your website.

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

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.

This topic contains 2 replies, has 2 voices.

Last updated by sianE 2 years ago.

Assisted by: Christopher Amirian.

Author
Posts
#2531265

Tell us what you are trying to do?

I am trying to use code snippets to set the default category for posts that are created using the toolset post forms. I have created one post form for Clubs that uses the Clubs category, and one for Sales that uses the Sales category.

I have used the following code snippet for the Clubs posts, which works correctly:

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"]==7220)
{
$category_ids = array(82); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'category');

}
}

When I use the same code for the Sales posts (but changing the form ID and category ID accordingly), the website breaks:

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"]==7232)
{
$category_ids = array(83); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'category');

}
}

How do I make both codes work? Why is the Sales code not working?

Is there any documentation that you are following?
https://toolset.com/forums/topic/set-default-category-for-posts/#post-389970
https://toolset.com/forums/topic/automatically-assigning-a-category-to-a-submission/

What is the link to your site?

hidden link

#2531687

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

You can not add an action two times in WordPress. You need to use both codes in one action.

So maybe something like this:

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"]==7220) {
		$category_ids = array(82); // replace your category ID
		wp_set_object_terms( $post_id, $category_ids, 'category');
	}

	if ($form_data["id"]==7232) {
		$category_ids = array(83); // replace your category ID
		wp_set_object_terms( $post_id, $category_ids, 'category');
	}	
}

So basically it is the same code but uses the IF statements inside the action altogether and you do not need to add another action call.

Thanks.

#2531847

Perfect thank you!