Skip Navigation

[Resolved] Generate a unic number when posting a new post

This support ticket is created 5 years, 9 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 5 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1230036

Hi
Is there a way to generate a unic number to a post when posting from frontend? This would serve as a kind of register, so the user posting the post can identify it later. This number should be visible in archive and single post view aswell.

I love this tool - it´s amazing 😉

#1230043

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Samuel,

Thank you for contacting our support forum.

Actually yes this is possible but it would be required to use a little custom code to do this. Is there a specific length of numbers you want to use ? e.g 4 digits or 5 digits

It should also be noted that if there isn't a specific length then the post id can be used since this is also a unique number as well.

Should you wish to generate the numbers differently please let me know and I should be able to write up some code to generate the random numbers for you.

Thanks,
Shane

#1230369

Hi Shane,
Some custom code would be great! May I opt in for both solutions; using post ID, and a custom code to use 5 digits.

Much appreciated!

#1231013

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Samuel,

I can show you both methods.

What you need to do is add one of the code below to our Toolset custom coding section in Toolset -> Settings -> Custom Code. Remember to activate it. For both methods you will see I have the form id == 12, just change this to your form ID that you are using.

The next thing is the "wpcf-custom-field" you will need to change this to your custom field slug keeping the "wpcf-" prefix.

Method 1 - Random 5 digit number.

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']==12)
    {
            $five_digit_random_number = mt_rand(10000,99999);
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-custom-field', $five_digit_random_number, true);
    }
}

Method 2 - Using the Post ID


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']==12)
    {
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-custom-field', $post_id, true);
    }
}

Please let me know if this helps.

Thanks,
Shane