Skip Navigation

[Resolved] Update Post Title with values from an existing Taxonomy Term

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

This support ticket is created 6 years, 3 months 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 2 voices.

Last updated by ScottM9386 6 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#608041

Hello,

Is it possible to auto fill a post title in a form with a just created category name?
For instance, I have a category that is a company name and I also need a post with of the same company name with details on the company. I don't want the user to have to type in the company name twice.
If I have a form where the user first creates the category "Company XYZ" and submits it, is it possible for this to become the title of a new Post?
I'm new at this so please bear with me.
Another option, is it possible for a user submitted form to contain the term fields or are term fields only on the back-end.
Thank you!

#608130

I'm using this in functions - can you for see any problems?

/**
* post title becomes a category
*/

add_action('save_post', 'add_title_as_category');

function add_title_as_category( $postid ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
$post = get_post($postid);
if ( $post->post_type == 'publisher') { // change 'post' to any cpt you want to target
$term = get_term_by('slug', $post->post_name, 'music-publisher');
if ( empty($term) ) {
$add = wp_insert_term( $post->post_title, 'music-publisher', array('slug'=> $post->post_name) );
if ( is_array($add) && isset($add['term_id']) ) {
wp_set_object_terms($postid, $add['term_id'], 'music-publisher', true );
}
}
}

// change taxonomy below to create same category in other taxonomy from same post title
if ( $post->post_type == 'publisher') { // change 'post' to any cpt you want to target
$term = get_term_by('slug', $post->post_name, 'alternate-title-1-publisher');
if ( empty($term) ) {
$add = wp_insert_term( $post->post_title, 'alternate-title-1-publisher', array('slug'=> $post->post_name) );
if ( is_array($add) && isset($add['term_id']) ) {
wp_set_object_terms($postid, $add['term_id'], 'alternate-title-1-publisher', true );
}
}
}

// last bracket below to end whole string
}

#608207

If I have a form where the user first creates the category "Company XYZ" and submits it, is it possible for this to become the title of a new Post?

Yes, this is possible in multiple ways.
What you need is to hook into the cred_save_data() documented here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This hook allows you to manipulate the Post at the moment CRED saved the Post to the Database.

This means, you can either grab the data from the form accessing $_POST, or from the database itself, and then updating the post in the database using the usual WordPress API.

As example to get the value from a Custom Field, filled during submit, and update the Title with it, you would use:
- data from $_POST or get_post_meta()
- wp_update_post() to update the Post Title

It's easiest to get the data from $_POST, you just need to access the right field slug.
Or, from the database, using the WordPress API.
For taxonomies they have this function:
https://codex.wordpress.org/Function_Reference/wp_get_post_terms

From that, you can use the below to update the post title:
https://codex.wordpress.org/Function_Reference/wp_update_post

Here are some reference snippet:
https://pastebin.com/KjW2y826
https://pastebin.com/2BTbVbcs
https://pastebin.com/A2UMXVUJ

Another option, is it possible for a user submitted form to contain the term fields or are term fields only on the back-end.

It is currently not possible to add, edit or else how manipulate User and Term Fields in the Front End with CRED, but both are requested features, and will be implemented at some point in the future.
I added your voice to the requests.

I'm using this in functions - can you for see any problems?

1. You are using save_post() which does not hook into CRED Code but into the WordPress code fired when a post is added or updated (usually in the backend)

2. The code to me seems to insert terms according values of other terms. Not what you want.

#609173

thank you!!

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