1

I am attempting to make one of my sections use auto-generated titles. I am making my first entry with a title like 8000. What I need is the second entry to be titled 8001, the third 8002, etc. Each entry must have a unique title in sequence.

This is the Title Format I tried using:

{{ craft.entries.section('scripts').last().title + 1 }}

I created a test entry after that and it successfully generated a title similar to the last entry plus one. However, when I created a second test entry, the title remained the same as the first test entry, but the slug was titlexx-1. I made another test entry, same thing, with the slug titlexx-2.

What am I missing in my Title Format?

Thanks for the help!

1 Answer 1

1

Something like this should do what you're looking for:

{% if not object.id %}{{ craft.entries.section('scripts').order('postDate DESC').first().title + 1 }}{% else %}{{ object.title }}{% endif %}
3
  • Is there a way to make this trigger ONLY when a brand new entry is saved? When the entry is updated and saved the title continues to be changed. Commented Jul 7, 2017 at 21:44
  • 1
    Updated answer...
    – Brad Bell
    Commented Jul 8, 2017 at 2:36
  • Amazing. Thank you so much! I had no idea that a field could be so powerful... Commented Jul 11, 2017 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.