Skip Navigation

[Resolved] Number will increment when editing post “Auto Numbering Posts”

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

Problem:
I would like to automatically increment a custom field value when I create a new post in my custom post type. The field value should be the highest field value from the other posts, plus one.

Solution:
You can use standard wordpress hook "save_post" to build such solution and add that hook to your current theme's functions.php file.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/number-will-increment-when-editing-post-auto-numbering-posts-vs2/#post-644615

Relevant Documentation:

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 18 replies, has 3 voices.

Last updated by JosV9233 6 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#631787

I have incorporated an incremental number in a post (see: https://toolset.com/forums/topic/auto-numbering-posts/). This works fine, except when I edit the record, then the number will increment also.
How can I stop this and only have this number assigned on creation?

Menu to follow is admin > zoeken. Then click on an order no. In the content template click edit. After saving this the number has incremented.

The code I used is:

/**
* Add an auto-incrementing ordernummer field to telorder posts
*/
function auto_assign_ids( $post_id, $post, $update ) {

// Only assign ID to new telorder posts
if ( $post->post_status == 'publish' && $post->post_type == 'telorder' ) {

// get the most recent telorder post
$telorder_args = array(
'numberposts' => 2,
'post_type' => 'telorder',
'orderby' => 'post_date',
'order' => 'DESC'
);
$telorders = get_posts( $telorder_args );

// get the project_id of the prior post
$last_id = get_post_meta( $telorders[1]->ID, 'wpcf-ordernummer', true );

if ( !$last_id ) {
$last_id = 0;
}

// increment
$last_id++;

// set the project_id of the current post
update_post_meta( $post_id, 'wpcf-ordernummer', $last_id );

}
}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );

#631842

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can wrap all your code by adding if condition to check $update is true or false.

You should try to use following code and try to resolve your issue.

function auto_assign_ids( $post_id, $post, $update ) {

if($update == false) {

// Only assign ID to new telorder posts
if ( $post->post_status == 'publish' && $post->post_type == 'telorder' ) {

// get the most recent telorder post
$telorder_args = array(
'numberposts' => 2,
'post_type' => 'telorder',
'orderby' => 'post_date',
'order' => 'DESC'
);
$telorders = get_posts( $telorder_args );

// get the project_id of the prior post
$last_id = get_post_meta( $telorders[1]->ID, 'wpcf-ordernummer', true );

if ( !$last_id ) {
$last_id = 0;
}

// increment
$last_id++;

// set the project_id of the current post
update_post_meta( $post_id, 'wpcf-ordernummer', $last_id );

}
}
}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );

More info:
=> https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

#631849

Hi Minesh,
Thank you for your quick reaction. Unfortunately on new orders it will not give an order number anymore.

#631851

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I just shared the information how you can check if you are updating the post or adding a new one.

If $update is true - that means you are updating existing post, and at that time you dont wanted to run that script - correct? If yes: thats the only change I've made and guided you about.

Regarding where you want to display something and whats not working - could you please give me exact information whats not working with all the workflow. I would be happy to help you further.

#631865

Hi Minesh,
I can see what you changed and do not understand why it does not work anymore.
To test I put back the old code and it worked again. Then I manually put in the [if($update == false) {] and the [}] in the right places without the [] and it stopped working.
I want to output an incremental number in a custom post type. I can send you login to check yourself.

#631883

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please tell me where you are displaying the field?

Also send me working FTP access details.

#632129
#632391

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Unfortunately - the wp-admin access details you shared is not working as this end.

Could you please resend working wp-admin and FTP details.

I have set the next reply to private which means only you and I have access to it.

#632396
#632404

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thanks wp-admin access details are working now but still FTP access details are not working.

Could you please resend it.

#632415
#632419

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

FTP access details still not working. Do I need to use any port?

#632421
#632426

> domains > zz6.nl > public.html > v-web

#632475

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - finally - FTP access working now.

Could you please point me the URL where you are displaying the field?

I can see view is added on this page: hidden link and it's displaying the order number: