Publish in past?

Is it possible to allow the setting of the publish date of an item (news article) in the past?

I am creating a new Umbraco site (my first. And I absolutely love it!) and porting content from our existing site, so we have articles going back a few years. I have added a publishDate field in order to get the date properly displayed, but because of examine, I cannot sort on this field in search results and would like the results to be in descending date order. So it’s not really a viable option.

I’ve tried writing an index component to convert the publishDate field to ticks, but can’t get this working either. - Even after rebuilding the index I don’t see any more properties - But that is a separate question I’ll be asking.

I’ve unpublished the article and tried a scheduled publish, but I get an error saying it can’t be published in the past? Is there anyway, short of a SQL query, to achieve this?

I don’t think so, you would be better off adding your own date field and use that.

As far as I’m aware, you can’t change the publish date, or create date, however if you add a property such as “date picker” then you will be able to render and use this. It should work the same and be pretty simple to set up.

This is pretty much what I’ve done, but the need to create a custom index in order to sort on it was something I was hoping to avoid.

Hello @TonyLaw12

  1. Add Published date field explicitly in document type
  2. Add this field in examine index and populate values.
  3. Perform sorting on this field, ticks value would be good option here rather than datetime format.

Hopefully this can help in your case :slight_smile:

If you did want an additional publishDate datetime property that allows sorting in examine…

Custom indexing | CMS | Umbraco Documentation

using Examine;
using Examine.Lucene;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;

namespace Umbraco.Docs.Samples.Web.CustomIndexing;

public class ConfigureExternalIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions>
{
    public void Configure(string name, LuceneDirectoryIndexOptions options)
    {
        if (name.Equals(Constants.UmbracoIndexes.ExternalIndexName))
        {
            options.FieldDefinitions.AddOrUpdate(new FieldDefinition("publishDate", FieldDefinitionTypes.DateTime));
        }
    }

    // Part of the interface, but does not need to be implemented for this.
    public void Configure(LuceneDirectoryIndexOptions options)
    {
        throw new System.NotImplementedException();
    }
}

public class ExamineOptionsComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        // Custom Examine configuration
        builder.Services.ConfigureOptions<ConfigureExternalIndexOptions >();
    }
}

and then in your examine query…

query.OrderBy(new SortableField("publishDate", SortType.Long));

As it’s a news Item you probably don’t want to use the umbraco publish date.. as that would change everytime an editor touched the content..

So it’s probably the CreateDate you’d use from the native properties again not ideal… or just as you’ve done a dedicated custom property that is the date to be associated with the news release.

Which is what you currently have..

If you really did want to use the CreateDate might be simpler to use a package that allows import mapping…
maybe too late now, but in terms of porting content… CMSImport | Umbraco Marketplace
Allows you to well import from various types, and you can map to the umbraco core CreateDate from one of your properties..

Also uSync | Umbraco Marketplace
could be of use.. creating a content export gives you a config file that you can manipulate and reimport after setting the createDate..

As both these packages leverage setting a createDate, also should be possible to write code to set the CreateDate yourself.

Thanks for the replies. I have stayed with the custom date and added a custom index for it converting it to ticks to sort against.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.