Skip Navigation

[Resolved] Auto increment Post Title in Admin Area

This support ticket is created 3 years 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 2 voices.

Last updated by simchaH 3 years ago.

Assisted by: Shane.

Author
Posts
#2238363

Hi,

I found this thread below, and was wondering if it is the correct piece of code for what I am looking to accomplish:

https://toolset.com/forums/topic/how-to-generate-post-title-from-custom-field-values/

I would like to auto-increment the standard WordPress title starting from a certain number (i.e. 10000) and each time admin submits a new post, it goes up by 1. I want to know if this would work hand in hand with the built in Toolset option of hiding the Title of a CPT. So I will hide it and have it auto-incremented so that the admin will never need to deal with it, yet there will be ID's to search from in case necessary.

Thank You,
Simcha

#2238389

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simcha,

Thank you for getting in touch.

Essentially you will want to start your post titles at 0 correct? Then whenever a new post is created that post will then be an increment of the first post's title ?

Will all the post titles essentially be numeric?

Please let me know.
Thanks,
Shane

#2238411

So I would like to start off at the number 10,000 because I am already importing 9000 posts to the CPT, and am going to import them directly to the WP Title field, so I would like to start from a unique non-duplicate number. From there I would like it to go up by 1 for each new post created.

Whether or not they would be completely numeric is a good question. I would primarily like to be able to append a two letter prefix dependent on the specific custom categories created (i.e. if admin selects the category "shoes", the auto-increment will be SH10001. If admin selects the category "heels" then it'll be HE10001). However, if that is not possible then I would keep it to just numbers, and have each new post go up incrementally numerically each time. (i.e. 10001, 10002, 10003).

Thanks!

#2238463

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simcha,

I understand now what you want to achieve, however this will be a highly customized solution which would fall outside of our support scope as the solution would be specific to your site.

There are 2 ways that we can proceed here. If you're familiar with PHP and coding then I can provide recommendations and resource materials and guide you through crafting the solution.

If you're not familiar with php and coding then it would be best to have a contractor craft up this custom solution for you.
Should this be the case you can seek assistance by going to the link below.
https://toolset.com/contractors/

Please let me know so I can further advise you.

Thanks,
Shane

#2238491

Thanks for your quick response Shane,

I would love to first give it a shot by myself, would you be able to start me off with some generic code or documentation to where I can find it? Does the above thread that I mentioned help me at all?

I would look into both solutions, either just adding incrementally without the letters in the beginning (which I'm assuming is more simple), and also the option for adding letters. But what I definitely need, is the ability to start from number 10,000.

Thanks so much

#2238537

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simcha,

Sure first you will perhaps need a custom field to store the current number of your post. This means that you'll have a number field to hold the record so it can be used to check against for the new post.

You will need to identify whether you're creating the post on the frontend or backend as the hooks will vary but the concept will remain the same. If you're using the backend then the hook you should be looking at is this one below.
https://developer.wordpress.org/reference/hooks/save_post/

Here is an example code of how you will set the post title.
hidden link

This should provide a suitable example on how to get a post meta value and append it to the post title.

Please let me know if this helps.
Thanks,
Shane

#2240003

Thanks for the info Shane, I'm going to take a look at the docs you sent me. Just to clarify, you mean I will have to first create a custom field, let's say call it "Title ID" and then from that custom field I will be sending the data over to the title?

I am potentially going to need this for the frontend form as well, is there a link to that too?

Thank you!

#2240023

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simcha,

let's say call it "Title ID" and then from that custom field I will be sending the data over to the title?

That's correct, so this field will be used to check against to auto increment the title for the new post. Essentially the process is going to be like this.

Submits new post -> Check previous post Title ID field to get the last reference number -> Auto increment the Title ID of the previous field by 1 -> Store this into the Title ID field for the new post as well as the Title of the post.

For our frontend forms you can use this hook below to hook into the submit data for the post.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Next you should be looking at this function as well to get the previously created post object.
https://stackoverflow.com/questions/6324421/getting-next-previous-post-id-using-current-post-id-in-wordpress

Please let me know if this helps.
Thanks,
Shane

#2240143

Got it, okay thanks for explaining so clearly. I just had one or two more questions:

The Title ID that we'll need in order to accomplish this, is there a way to hide it from the admin so that they don't get confused about what is supposed to get entered there? It really is only necessary for this piece of code, but otherwise it should be hidden correct?

#2240671

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simcha,

In order to disable the title field you will need to do it through the post type settings. This can be done by going go to Toolset -> Post Types and edit the custom post type.

From there go to "Sections to display when editing" and disable Title.

This should remove the post title field from your CPT so that admin cannot edit it.

Thanks,
Shane

#2240733

My issue is resolved now. Thank you!