Auto numbering posts
Author:
Nigel (Toolset moderator)
Description:
Add the php code to your theme’s function.php.
In the code there is a CPT with a slug of “telorder” and you have to add a custom field with a slug of “ordernummer”.
Make link of ordernummer to go straight to the post (only for Toolset):
<a href=”[wpv-post-url]”>[types field=’ordernummer’]</a>
Kind:
PHP, Toolset
PHP:
/**
* 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 );