Is there a way to ge tthe ID number on this page, hidden link the auto generate based on the order it is in? Right now I have a custom field setup and we manually input the number but I want it to be automatic. The reason is if I have 20 lines of posts, and I delete one of them, it will skip it. But I want to have them in numerical order.
The ID is a custom field on some custom post type, yes?
And you have a view which lists these custom posts, ordered by the ID custom field.
And you want to auto-generate these ID numbers so that if new posts are added they are automatically assigned the next number in the sequence. And if any posts are deleted then subsequent numbers are resequenced, i.e. if post ID =12 is deleted then post ID = 13 becomes 12 etc.
Have I understood you correctly?
That will involve writing some custom code, but it shouldn't be too challenging, I can help you with it.
The ID numbers are meaningful in terms of their display on that page, yes? You are not just looking at a numbered list where the number is not so important?
I just started thinking about the implementation of this and I want to double-check the re-assignment of IDs when a post is deleted.
If the IDs are going to be referenced, and the IDs are going to change whenever a post has been deleted, is that going to create any problems? That you might be referring to a post by this ID when that ID might change?
I was thinking that too and I have decided to nix that idea of referencing them because of the deleting of rows could mess it up. I STILL want to do what I initially posted about which is incremental ID's automatically.
Try adding the following code to your theme's functions.php file.
Note that in my example I have made a custom post type with a slug of "project" and added a custom field to projects with a slug of "project-id". You will need to edit the code accordingly.
/**
* Add an auto-incrementing Project ID field to Project posts
*/
function auto_assign_ids( $post_id, $post, $update ) {
// Only assign ID to new project posts
if ( $post->post_status == 'publish' && $post->post_type == 'project' ) {
// get the most recent Project post
$project_args = array(
'numberposts' => 2,
'post_type' => 'project',
'orderby' => 'post_date',
'order' => 'DESC'
);
$projects = get_posts( $project_args );
// get the project_id of the prior post
$last_id = get_post_meta( $projects[1]->ID, 'wpcf-project-id', true );
// increment
$last_id++;
// set the project_id of the current post
update_post_meta( $post_id, 'wpcf-project-id', $last_id );
}
}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );
//APL ID Increments
/**
* Add an auto-incrementing Project ID field to Project posts
*/
function auto_assign_ids( $post_id, $post, $update ) {
// Only assign ID to new project posts
if ( $post->post_status == 'publish' && $post->post_type == 'approved-promotion' ) {
// get the most recent Project post
$approved-promotion_args = array(
'numberposts' => 2,
'post_type' => 'approved-promotion',
'orderby' => 'post_date',
'order' => 'DESC'
);
$projects = get_posts( $approved-promotion_args );
// get the project_id of the prior post
$last_id = get_post_meta( $approved-promotion[1]->ID, 'wpcf-id-no', true );
// increment
$last_id++;
// set the project_id of the current post
update_post_meta( $post_id, 'wpcf-id-no', $last_id );
}
}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );
In PHP you can't have a variable name with a hyphen, you use underscores instead.
Try the following:
function auto_assign_ids( $post_id, $post, $update ) {
// Only assign ID to new approved promotion posts
if ( $post->post_status == 'publish' && $post->post_type == 'approved-promotion' ) {
// get the most recent promotion posts
$promotion_args = array(
'numberposts' => 2,
'post_type' => 'approved-promotion',
'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-no', true );
// increment
$last_id++;
// set the project_id of the current post
update_post_meta( $post_id, 'wpcf-id-no', $last_id );
}
}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );
That seems to have worked like a charm. I just got word from the other devs on the site that we won't be deleting posts so the numbering system won't have to change when a post gets deleted.
Saying that, is there now a way to reference the ID number to link to so it goes straight to it?