Skip Navigation

[Resolved] Auto Numbering Posts

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

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 12 replies, has 2 voices.

Last updated by Nigel 7 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#440023
2016-09-26 10_01_05-Approved Promotions List - THE RESOURCE SITE FOR X THEME USERS.jpg

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.

#440225

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Donald

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?

#440338

All of that is correct.

The ID numbers are meaningful as they are going to be referenced.

#440401

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Donald

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?

#440402

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.

#440439

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Donald

That makes it a little more straightforward.

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 );

Let me know how you get on.

#440449

This is what I tried and got an error.

//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 );
#440458

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

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 );
#440460

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?

#440463

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I'm glad it worked for you.

Can you clarify what you mean by the linking?

If you are talking about generating the table shown in the photo in your original question, presumably you are generating that table with a view.

In the loop output section you can output the ID using a types shortcode (e.g. [types field='id-no']).

That will be outputting your ID for the current post in the loop, so you can use the views shortcodes to wrap it in a link, e.g.

<a href="[wpv-post-url]">[types field='id-no']</a>

Is that what you meant?

#440475

All good. Thank you.

#440485
2016-09-27 13_44_44-Approved Promotions List - THE RESOURCE SITE FOR X THEME USERS.jpg

Before finishing this ticket. Something is going on with the ID's now.

hidden link

#440674

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Donald

I didn't make any special provision for the first project.

Add the following few lines directly before the line with "// increment" and that should fix it.

		if ( !$last_id ) {
			$last_id = 0;
		}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.