How to generate sequential numbering in wordpress

2019-06-07 13:56发布

I'm testing the capabilities of WordPress and trying to build a kind of back office/workflow system. The basic principle is that each post will represent a job/spec that fall into certain categories etc etc... So far it's working great, but I'm having trouble with generating sequential job numbers.

The idea is that when the user posts a spec, WP automatically generates a unique 4 digit ID that will represent the job number of that spec.

The current solution only works as far as generating id's every time a post is called up in different categorised circumstances etc. What I want it to do is generate the number upon posting, then write to the database permanently rather than generate with the PHP.

Does anyone have a clue how I can do this?

1条回答
家丑人穷心不美
2楼-- · 2019-06-07 14:12

Since you said you can't use the WordPress post IDs, you could create a table that uses an AUTO_INCREMENT column to create and store them. After inserting a new entry there, you can retrieve the generated ID via PHP:

$lastid = $wpdb->insert_id;

This way you can let MySQL handle the generation of IDs and leave PHP out of it.

查看更多
登录 后发表回答