I have a post form that uses a field "ID". I need this field to contain a sequential number for every post. The first time the form is saved this field should be 1, then the second post that it creates would have an ID of 2, etc.
Some time ago, a toolset support staff provide me this code but I never used it because I couldn't get the rest of the form to work. Now I'm forced to use Toolset forms for creating new posts. I implemented the code, but it does nothing.
add_action('cred_before_save_data_18074', 'before_save_data_for_form_with_id_18074',10,1);
function before_save_data_for_form_with_id_18074($form_data)
{
if ( $post->post_type == 'trip-request' ) {
// get the most recent promotion posts
$promotion_args = array(
'numberposts' => 2,
'post_type' => 'trip-request',
'orderby' => 'post_date',
'order' => 'DESC'
);
$promotions = get_posts( $promotion_args );
// get the project_id of the prior post
$last_id = get_post_meta( $promotions[1]->ID, 'wpcf-id', true );
if ( !$last_id ) {
$last_id = 0;
}
// increment
$last_id++;
// set the project_id of the current post
update_post_meta( $post_id, 'wpcf-id', $last_id );
}
}
Hello. Thank you for contacting the Toolset support.
I am not sure why you are using the following line of code as there is no $post object variable is available or defined.
if ( $post->post_type == 'trip-request' ) {
Based on the hook I see that you attached the hook to your form ID 18074: cred_before_save_data_18074 - so the hook will run form form ID 18074 onlyand a form can have only one post type attached at a time so I wonder why you added the check for post type condition:
if ( $post->post_type == 'trip-request' ) {
I think you should try to remove that conditional statement and check if the following code help you to resolve your issue:
add_action('cred_before_save_data_18074', 'before_save_data_for_form_with_id_18074',10,1);
function before_save_data_for_form_with_id_18074($form_data) {
// get the most recent promotion posts
$promotion_args = array(
'numberposts' => 2,
'post_type' => 'trip-request',
'orderby' => 'post_date',
'order' => 'DESC'
);
$promotions = get_posts( $promotion_args );
// get the project_id of the prior post
$last_id = get_post_meta( $promotions[1]->ID, 'wpcf-id', true );
if ( !$last_id ) {
$last_id = 0;
}
// increment
$last_id++;
// set the project_id of the current post
update_post_meta( $post_id, 'wpcf-id', $last_id );
}
I removed that condition as suggested and filled out the form again to generate a new post. The ID field was still not filled in.
This code may be all wrong—it was provided by another Toolset support rep, I did not write it. Is there a better way of achieving what I need? I notice a lot of people asking for the same thing (unfortunately none of the answers I found were understandable), so it's a shame that this is not a built-in function as it's something so typical (which is why virtually every form builder plugin offers a feature like this).
Whatever. How can I generate a unique, incremental number for each new post?
I will be happy to guide you with the correct code implementation with your form.
To ensure that where-on what form you want to implement the sequential value for your custom field. Can you please confirm you want to implement that on the following form submit: hidden link
I've added a better code that will minimise the performance issue as this method will not have to fire a query.
For example:
- As you can see with the following code - we are adding a new option "cred_form_18074_cf_id_value" and assign it a value 1 if the option "cred_form_18074_cf_id_value" is not found. If the option is found, we are adding +1 to the option value for option "cred_form_18074_cf_id_value".
I've added the code to the custom code section offered by Toolset:
=> hidden link
Can you please try and check if this works as when I try to click on "Next" button on first step of form it throwing the JS errors on browser's console and I could not able to move to next step.