• Hi,
    I need help with my custom post. What i’m trying to achieve is:

    1> when a new custom post is created, the post title should be automatically created with a define text(title) + auto increment number

    eg:
    define text = “Custom Post#”
    starting number = 100
    SO THAT THE:

    first custom post, title should be: “Custom Post# 101”
    second custom post title should be: “Custom Post# 102” …
    third should be “Custom Post# 103” …and so forth…

    How can I go about to achieve this task please

    Much appreciated

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You can force the title and post name (slug) be whatever you like (that’s not already taken) through the ‘wp_insert_post_data’ filter. This would not be reflected on the editor screen though until the page reloads. To get the editor screen to default to the right title from the start will probably require JavaScript of some sort. The specifics depend on the editor being used. Even with an editor default, you’ll still need the ‘wp_insert_post_data’ enforcement so users cannot alter the title through the editor or through more nefarious means. You could use JS to make the title field read-only, but you still would want the ‘wp_insert_post_data’ enforcement in case someone is more nefarious in their efforts.

    You’ll need to store the next available number as an option value and auto-increment it every time a new post is created. Doing so from the ‘wp_insert_post_data’ callback will work, but post insertion isn’t guaranteed at this point, so there could be gaps in the numbering if incrementing succeeds but post insertion does not. If sequential numbering is required, use a hook that fires after successful insertion, like “save_post_{$post->post_type}”.

    Be sure you only set the title and increment the number for new posts, not for updates.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Post auto generate post title incrementally’ is closed to new replies.