Setting PublishDate in an archetype?

I’m attempting to ensure that the PublishDate is set on content when it is generated with hugo new. What do I need to put in the frontmatter of the archetype to get this parameter set?

@mattstratton you have to create a new file in archetype directory named as default.md

and can put any information, normally date is automatically added when you create archetype for post.

+++
tags = [“x”, “y”]
categories = [“x”, “y”]
PublishDate = “”
+++

Check normally PublishDate is automatically added but if you want you can assign it too.

Yeah, we have custom archetypes all over the place, but PublishDate never gets set. I’ll try adding a blank field for it.

@mattstratton Any luck with this? I have three date parameters: date,publishdate, and updated in a theme I’m working on. If I don’t set the field to anything I get null

I attempted this. I have an archetype defined at archetypes/speaker.md with the following contents:

+++
Title = ""
Twitter = ""
Homepage = ""
PublishDate = ""
+++

Then I run this command: hugo new events/2017-ponyville/speakers/rainbow-dash.md -k speaker

This results in the following content at content/events/2017-ponyville/speakers/rainbow-dash.md

+++
Homepage = ""
PublishDate = ""
Title = "rainbow dash"
Twitter = ""
date = "2016-12-08T20:55:58-06:00"

+++

So it seems that PublishDate doesn’t actually get set after all.

This isn’t necessarily completely the end of the world for us, as we have our content folks add content via a shell script rather than calling hugo new directly, so we can do a replace or something as part of the script, but it would be nice to have.

1 Like

Did you ever figure this out? The default field in the default archetype that is now built in Hugo (v.24.1) for me looks like this:

---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
draft: true
---

So maybe putting that {{ .Date }} in there will do what you want with publishdate =

(Not tested) you could also try using the AddDate function :smile:

Your example has publishDate set to blank, which matches your output just fine. And @gaetawoo is right, Hugo >= 0.24 has much more powerful archetype handling – with template support and, most notable: Other than the template parsing, we just use the archetype file as-is, so what you put in there is what you will get in the resulting content file.

1 Like