Hi, I am trying to build a custom title for the custom type Fascicoli (slug "fascicolo") when saving.
My intention is to compile a string with 3 fields:
Title of a related post (relationship: rivista-fascicolo)
Content of the custom field "anno-solare"
Content of the custom field "numero-fascicolo"
I built a plugin with this code but nothing happens:
//Create a dynamic post title by the CRED form.
add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
if ($form_data['id']==56) {
$title ="";
$rivista_id = toolset_get_related_post( $post_id, $relationship='rivista-fascicolo' );
$rivista = get_the_title($rivista_id);
$anno_solare = get_post_meta($post_id, 'wpcf-anno-solare', true);
$numero_fascicolo = get_post_meta($post_id, 'wpcf-numero-fascicolo', true);
$title= $anno_solare . '-' . $numero_fascicolo;
$args = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => sanitize_title( $title )
);
wp_update_post($args);
}
}
In the past I built something similar using ACF hook 'acf/save_post' and was ok.
My only doubt is the form_data id. I got it from the Toolset custom field group that contains all the fields and that is linked to my custom type Fascicoli. Is this right?
Hello. Thank you for contacting the Toolset support.
The screenshot you shared belongs to custom field group not Toolset forms.
As I understand, you have a Toolset post form where you want to apply the "cred_save_data" hook to customize your title dynamically.
Actually, to locate your form ID, you should navigate to Toolset => Post Forms and grab the ID of your form and replace that within the code you shared. It should work.
If I misunderstood your requirement, please feel free to correct me.
I've reopened the ticket beacuse I studied new Forms plugin but maybe it's useless for what I need (please correct me if I'm wrong).
At this point I don't know if the cred_save_data hook is the solution for me.
There's a way to save a custom post type title compiling a string with 3 custom fields (one is a relationship)?
I wish obtain this result while saving the post from the backend.
Thanks in advance!
Hi Minesh,
I tried to build a plugin with save_post action for the backend and it works!! Thank You for the advice.
Now I have to customize the title with a form on the frontend too, so in this case cred_save_data would be the right choice.
I created a Post Form on a page for the insert module but it doesn't works: instead of writing the name of the post in the relationship, it writes the title of the page where the form is.
Can you please share the problem URL where you want to build the dynamic title as well as admin access details so I check check further.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Hi Minesh,
considering the values of the screenshot I woud like this: The Bamba - 2011 - 2323232 (for insert and update)
I know that with Toolset is possible insert custom code in a specific place. It is my habit to create plugin because I usually work with ACF and Oxygen. Oxygen delete all "ordinary" templates and has no a functions.php file. At the moment I'm using WP Ocean and Elementor but is possible I'll use Oxygen also for this site. I have to decide.
This is my first experience with Toolset
Hi Minesh, if you need further explanations about this, feel free to ask.
What do you mean with "As you are using the Toolset form, you need to use the Toolet Form's hook to build the post title"?
Is not cred_save_data a Toolset forms hook?
You should simply deactivate the plugin you have created as I moved the code to "Custom Code" section offered by Toolset.
I've added the following code to the "Custom Code" section offered by Toolset (it's a good practice to add Toolset related custom code to "Custom Code" section)
=> hidden link