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
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.