Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Scott 95 posts 277 karma points
    Oct 01, 2015 @ 08:47
    Scott
    0

    Sort by first publish date

    Hi,

    I am wondering if anybody knows how to sort sub pages by first published date. Thus neither createDate nor updateDate.

    Scott

  • Kevin Jump 2311 posts 14697 karma points MVP 7x c-trib
    Oct 01, 2015 @ 12:26
    Kevin Jump
    1

    Hi

    as far as i know there isn't a published date set when an item is publiushed, you could in theory go through the db and find them, but that would probibly involve hitting the database quite a bit and slow everything down.

    Instead you could create your own publish date property and then set it on the first publish of an item.

    For the code below you can create the "firstPublished" property on any doctype the same way as any other property, and then some code can set the value on publish.

    if you put something like below in app_code

    using System;
    using Umbraco.Core;
    using Umbraco.Core.Services;
    
    namespace UmbracoPackages.App_Code
    {
        public class PublishDate : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Publishing += ContentService_Publishing;
            }
    
            private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                foreach(var item in e.PublishedEntities)
                {
                    if (item.HasProperty("firstPublished"))
                    {
                        var pubDate = item.GetValue<DateTime>("firstPublished");
                        if (pubDate != null && pubDate != DateTime.MinValue)
                        {
                            item.SetValue("firstPublished", DateTime.Now);
                            ApplicationContext.Current.Services.ContentService.SaveAndPublishWithStatus(item);
                        }
                    }
                }
            }
        }
    }
    

    I haven't actually tested this, but the theory is.

    The code above will get called when any content is published. If the publishing item has a PublishedDate property and it's not set, set it to now, and then publish it.

    There may be a need to cancel the publishing event, when you change something, but i am not sure.

    from your front end razor you then have a FirstPublished property that you can sort anything by

    items.children().Where(....).OrderBy("firstPublished"); 
    

    or some such order query depending on what you are doing..

    Kevin

  • Scott 95 posts 277 karma points
    Oct 02, 2015 @ 09:51
    Scott
    0

    Hi Kevin,

    I catch the idea, I'll give it a look and see if it works as expected.

    Seems like it'll do just the job.

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft