Skip Navigation

[Resolved] Automatically Categorizing CRED Form Submissions

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

Problem:

Automatically categorize a blog post CRED form submission to a Category term called

Solution:

You can use WP function wp_set_object_terms() to setup the post term.

Relevant Documentation:

https://developer.wordpress.org/reference/functions/wp_set_object_terms/

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.

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)

This topic contains 3 replies, has 2 voices.

Last updated by zacharyL 1 year, 1 month ago.

Assigned support staff: Luo Yang.

Author
Posts
#2212175

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 hidden link. 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!

#2212559

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

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

#2213347

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

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.

#2216027

Great, thank you! It hadn't occurred to me that it would want the slug of the taxonomy. Thanks for your help!