Good afternoon,
I'm trying to create a random number that is automatically generated in a custom type when I hit a button.
I was looking at other related information,
https://toolset.com/forums/topic/generate-a-unic-number-when-posting-a-new-post/
and I created this code. But how do I link it to a button? And how i can use this number like a custom type title?
//NÚMERO ALEATORIO BONO
//
add_action('cred_save_data', 'id_for_posts_rand_seven_digit',10,2);
function id_for_posts_rand_seven_digit($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==5908)
{
$seven_digit_random_number = mt_rand(1000000,99999999);
// add it to saved post meta
add_post_meta($post_id, 'wpcf-referencia-del-bono', $seven_digit_random_number, true);
}
}
Hello,
There isn't such kind of built-in feature within Toolset plugins.
And the custom codes you mentioned above is for creating a new post.
I assume you are going to update existed posts, if it is, you can try these:
1) Create a post form for editing post, so you will can setup submit button in the form
2) Create another custom codes, use update_post_meta() to update the custom field value, for example:
add_action('cred_save_data', 'update_id_for_posts_rand_seven_digit',10,2);
function update_id_for_posts_rand_seven_digit($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==123) // replace 123 with your edit post form ID
{
$seven_digit_random_number = mt_rand(1000000,99999999);
// add it to saved post meta
update_post_meta($post_id, 'wpcf-referencia-del-bono', $seven_digit_random_number);
}
}
3) If you want to update the post title too, please try WP function wp_update_post(), see WP document:
https://developer.wordpress.org/reference/functions/wp_update_post/
and example in above document:
https://developer.wordpress.org/reference/functions/wp_update_post/#user-contributed-notes
Thanks Luo.
I think I explained wrong
My idea is to put generate button and that action creates a code with a random number and with the date automatically that I have already done.
that number has to be the title of the custom type created and it must be unique
Could you help me?
thanks
Thanks for the details, the random number won't be unique, within WordPress, you can use post ID as unique number, you can use Views shortcode [wpv-post-id] to display it in front-end, see our document:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-id
Hi Luo,
my code to add is this
//REFERENCIA AUTOMÁTICA
add_action('cred_save_data', 'id_for_posts_using_post_id',10,2);
function id_for_posts_using_post_id($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==5908)
{
// add it to saved post meta
add_post_meta($post_id, 'wpcf-referencia-del-bono', $post_id, true);
}
}
But i dont know why is wrong. Could you help me?
otherwise this code is accepted but nothing happens.
//REFERENCIA AUTOMÁTICA
add_action('cred_save_data', 'id_for_posts_rand_five_digit',10,2);
function id_for_posts_rand_five_digit($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==5908)
{
$five_digit_random_number = mt_rand(10000,99999);
// add it to saved post meta
add_post_meta($post_id, 'wpcf-referencia-del-bono', $five_digit_random_number, true);
}
}
Since it is a custom 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 to test and debug it in a live website. thanks
My issue is resolved now. Thank you!