I'm trying to automatically categorize a blog post CRED form submission to a Category term called "Community News".
I realize that Toolset is inherently unable to do this out of the box, but I was able to find a support thread that provided a PHP snippet to help me along this path. This however, didn't seem to work.
// Assign Guest Blog Posts to Community News
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"]==2473)
{
$category_ids = array(1008); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'community-news');
}
}
I replaced the data here with proper IDs and taxonomy term slugs.
2473 = CRED form ID
1008 = "Community News" term ID (I tried using the default term ID [1] here, and it didn't make a difference)
community-news = "Community News" term slug
My site can be found at lien caché. You'll have to be able to login if you want to submit a post, however.
Any help in this manner would be greatly appreciated, thanks!
Hello,
Since it is a custom PHP codes problem, please provide a test site with the same problem also point out the problem page URL and form URL, where I can edit your custom PHP codes, I need a live website to test and debug this issue
Thanks for the details, I have done below modifications in your website:
replace this line from:
wp_set_object_terms( $post_id, $category_ids, 'community-news');
To:
wp_set_object_terms( $post_id, $category_ids, 'category');
The third parameter 'category' is the taxonomy slug.
See WP document:
https://developer.wordpress.org/reference/functions/wp_set_object_terms/
$taxonomy
(string) (Required) The context in which to relate the term to the object.
Great, thank you! It hadn't occurred to me that it would want the slug of the taxonomy. Thanks for your help!