<p>Unless I'm missing something, you should be able to use the <code>seq</code> Twig function (<a href="https://craftcms.com/docs/3.x/dev/functions.html#seq" rel="nofollow noreferrer">docs</a>) and use something like:</p>
<pre><code>{{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }}
</code></pre>
<p>This would generate the format you need <code>YYYYMMDD-NNN</code> and reset the <code>NNN</code> every <em>new</em> day.</p>
<p>Sequences are stored in your <code>sequences</code> database table.</p>

CC BY-SA 4.0
2021-03-09T13:48:24.100Z
Wow - this is brilliant! Never knew Twig had this capability under the hood. It's incrementing now, however, still a minor issue. I nested your code in the conditional  `{% if object.title %}  {{ object.title }} {% else %} {{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }} {% endif %}`. When I create a new entry, it increments (can see it at top). When I save the same entry, it increments yet again! Is my conditional wrong?
Ed Hebert
CC BY-SA 4.0
2021-03-09T14:00:05.287Z
I should also mention that I also tried `{% if object.id %} {{ object.title }} {% else %} {{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }} {% endif %}`. When creating the entry, the numerical title appears properly at the top of the entry page. However, when I save it, the entry is saved with the title "Untitled entry".
Ed Hebert
CC BY-SA 4.0
2021-03-09T07:31:03.920Z
2021-03-09T07:31:03.920Z
Oli
<p>Here's my solution:</p>
<pre><code>{% if object.title %}
  {{- object.title -}}
{% else %}
  {%- set date = 'now'|date('Ymd') -%}
  {%- set count = craft.entries.section('inventory').search('title::' ~ date ~ '*' ).anyStatus().count() + 1 -%}
  {{- date ~ '-' ~ (&quot;%03d&quot;|format(count)) -}}
{% endif %}
</code></pre>
<p>First, we're checking to see if the entry already has a title. If so, use the existing title since we only ever want it generated once.</p>
<p>The real trick is passing the current date formatted like <code>YYYYMMDD</code> to a <a href="https://craftcms.com/docs/3.x/searching.html" rel="nofollow noreferrer">search query</a>. For example, <code>.search('title::20210308*')</code> with search for all entries where the title field starts with <code>20210308</code>. Then we use <a href="https://craftcms.com/docs/3.x/element-queries.html#count" rel="nofollow noreferrer"><code>.count()</code></a> to find out how many entries exist and add 1 to that number.</p>
<p>Finally the <a href="https://twig.symfony.com/doc/2.x/filters/format.html" rel="nofollow noreferrer">format filter</a> adds leading zeros to the number (taken from this answer&quot;<a href="https://stackoverflow.com/a/25161326/1013978">Keep leading 0 in Twig</a>&quot;).</p>
<p>I've added whitespace control dashes (<code>{{- -}}</code>, <code>{%- -%}</code>) to prevent extra whitespace from creeping is, but you could just as easily wrap the whole thing in <code>{% apply spaceless %}...{% endapply %}</code> tags.</p>
<h2>Update:</h2>
<p>I realized there's a scenario where this solution wouldn't work. If entries are created on the same day but some are deleted, the <code>.count()</code> could be incorrect. For example, 2 entries are created (-001 and -002), but the first is deleted. The next entry created on that day will be numbered -002 as well, resulting in a duplicate title.</p>

CC BY-SA 4.0
2021-03-09T05:21:57.883Z
2021-03-09T21:14:38.667Z
2021-03-09T21:14:38.667Z
Alex Roper
<p>I might be pushing the limits of what's possible with Craft's autogenerated title capability, but in any event I'd like to figure out the best path to arrive at my goal.</p>
<p>We have an internal parts inventory with unique IDs assigned in the format YYYYMMDD-NNN. I'd like to recreate that same autogenerated format as we move our data to Craft.</p>
<p>Logically, the concept is &quot;today's date plus the sequential ID incrementing for the parts recorded one that day&quot;. Each day, we start back at  '-001' (e.g., '20210305-001', '20210305-002',...).</p>
<p>I've tried the following code in Title Format autogeneration input, but either have the syntax messed up or am pushing beyond the capability of what's possible with autogenerating for a title field in craft.</p>
<pre><code>{# Desired unique ID format = 'YYYYMMDD-NNN' #}
{% if not object.id %}
    {% set uniqueID = craft.entries.section('inventory')
    .order('postDate DESC')
    .first()
    .title %}
    {% set datePortion =  uniqueID | slice(0, 8) %} {# extract YYYYMMDD from string #}
    {% if datePortion == 'now'|date('Y-m-d') %}
        {% set count = uniqueID | slice(9, 3) %}
        {{ datePortion ~ '-' ~ count + 1 }} {# increment today's count for next part #}
    {% else %}
        {{ 'now'|date('Y-m-d') ~ '-001' }} {# first entry in a new day #}
    {% endif %}
{% else %}
    {{ object.title }}
{% endif %}
</code></pre>
<p>Hope someone has a more clever and elegant solution - thanks!</p>

<auto-generate>
Autogenerate complex Date / Count based Title
# Autogenerate complex Date / Count based Title

(Asked by: username_0 on 2021-03-05)

I might be pushing the limits of what's possible with Craft's autogenerated title capability, but in any event I'd like to figure out the best path to arrive at my goal.
We have an internal parts inventory with unique IDs assigned in the format YYYYMMDD-NNN. I'd like to recreate that same autogenerated format as we move our data to Craft.
Logically, the concept is "today's date plus the sequential ID incrementing for the parts recorded one that day". Each day, we start back at  '-001' (e.g., '20210305-001', '20210305-002',...).
I've tried the following code in Title Format autogeneration input, but either have the syntax messed up or am pushing beyond the capability of what's possible with autogenerating for a title field in craft.
{# Desired unique ID format = 'YYYYMMDD-NNN' #}
{% if not object.id %}
    {% set uniqueID = craft.entries.section('inventory')
    .order('postDate DESC')
    .first()
    .title %}
    {% set datePortion =  uniqueID | slice(0, 8) %} {# extract YYYYMMDD from string #}
    {% if datePortion == 'now'|date('Y-m-d') %}
        {% set count = uniqueID | slice(9, 3) %}
        {{ datePortion ~ '-' ~ count + 1 }} {# increment today's count for next part #}
    {% else %}
        {{ 'now'|date('Y-m-d') ~ '-001' }} {# first entry in a new day #}
    {% endif %}
{% else %}
    {{ object.title }}
{% endif %}

Hope someone has a more clever and elegant solution - thanks!

**Tags:** auto-generate


---

## 2 answers

### Answer by username_1 on 2021-03-09. Score: 3 | Accepted answer

Unless I'm missing something, you should be able to use the seq Twig function (docs) and use something like:
{{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }}

This would generate the format you need YYYYMMDD-NNN and reset the NNN every new day.
Sequences are stored in your sequences database table.

> **Comments:**
> **username_0**: Wow - this is brilliant! Never knew Twig had this capability under the hood. It's incrementing now, however, still a minor issue. I nested your code in the conditional  `{% if object.title %}  {{ object.title }} {% else %} {{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }} {% endif %}`. When I create a new entry, it increments (can see it at top). When I save the same entry, it increments yet again! Is my conditional wrong?  
> **username_0**: I should also mention that I also tried `{% if object.id %} {{ object.title }} {% else %} {{ 'now'|date('Ymd') ~ '-' ~ seq('now'|date('Ymd'), 3) }} {% endif %}`. When creating the entry, the numerical title appears properly at the top of the entry page. However, when I save it, the entry is saved with the title "Untitled entry".  

---

### Answer by username_2 on 2021-03-09. Score: 2

Here's my solution:
{% if object.title %}
  {{- object.title -}}
{% else %}
  {%- set date = 'now'|date('Ymd') -%}
  {%- set count = craft.entries.section('inventory').search('title::' ~ date ~ '*' ).anyStatus().count() + 1 -%}
  {{- date ~ '-' ~ ("%03d"|format(count)) -}}
{% endif %}

First, we're checking to see if the entry already has a title. If so, use the existing title since we only ever want it generated once.
The real trick is passing the current date formatted like YYYYMMDD to a search query. For example, .search('title::20210308*') with search for all entries where the title field starts with 20210308. Then we use .count() to find out how many entries exist and add 1 to that number.
Finally the format filter adds leading zeros to the number (taken from this answer"Keep leading 0 in Twig").
I've added whitespace control dashes ({{- -}}, {%- -%}) to prevent extra whitespace from creeping is, but you could just as easily wrap the whole thing in {% apply spaceless %}...{% endapply %} tags.
Update:
I realized there's a scenario where this solution wouldn't work. If entries are created on the same day but some are deleted, the .count() could be incorrect. For example, 2 entries are created (-001 and -002), but the first is deleted. The next entry created on that day will be numbered -002 as well, resulting in a duplicate title.


---

