Re:

How can I sort collection items by publish date, not created date?

thevault
Excursionist
12 0 10

Hi! I need to be able to sort items in a collection by publish date NOT by created date. Are there any apps for this or is there a reasonably easy way to do this by adjusting the code for that collection? We use the debut theme.  I see a lottttt of people have asked for this feature over a lot of years, but am wondering if Shopify has added any way to do it for this since the last person asked.

Replies 28 (28)

Lixon_Louis
Shopify Partner
1193 35 268

@thevault You can use {% assign collections = collections | sort: 'published_at'%}

For example, the code responsible for https://nichegeek.myshopify.com/collections/  would be as given below (sections/list-collections-template.liquid)

{% assign collections = collections | sort: 'published_at'%}
        {% for collection in collections %}
        {% if collection.products_count != 0 %}
          <li class="grid__item {{ grid_item_width }}">
            {% include 'collection-grid-item', collection_image_size: image_size %} 
             {{collection.published_at | date: "%a, %b %d, %Y"}}
          </li>
    {% endif %}
{% endfor %}

 

 

 

thevault
Excursionist
12 0 10
Oh I am not looking to sort my list of collections....I’m looking to sort a collection’s products by their publish dates
Lixon_Louis
Shopify Partner
1193 35 268

@thevault Then also the logic remains the same. use sort filter on collection.products object

 

 {% assign collection = collection.products | sort: 'published_at'%}
        {% for product in collection %}
          <li class="grid__item grid__item--{{section.id}} {{ grid_item_width }}">
            {% include 'product-card-grid', max_height: max_height %} {{product.published_at | date: "%a, %b %d, %Y"}}
          </li>
     {%endfor%}

 

thevault
Excursionist
12 0 10

Cool that looks like it should work but I've minimal code experience...so I'm not sure where to paste it in when I click edit code. I don's see anything obvious in liquid called a "collections.product" object

 

Is there somewhere particular to put this to replace the 'created_at' command in my current collection template ? or do I need to create a new template or something?

 

I can't imagine ever wanting to sort products by creation date rather than publish date, so I'm ok with just changing that sort command to 'publish_date' site-wide,  But if it is easier to do it for just one collection that's cool too. need this working soon so whatever is easier

Lixon_Louis
Shopify Partner
1193 35 268

@thevault It depends on where you want this sorting to happen. For example if its the /collections/all the code responsible for that page is in sections/collection-template.liquid. It should be somewhere between line 145 - 170, but i'm not sure because of which version of Debut you are using.

 

 

thevault
Excursionist
12 0 10

Thanks! I found it, and it definitely changed the sorting but to what I can't tell... Items def aren't in descending order by publish date because some from 4/17 are at the top of the first page and 4/18 is after...there are also strange empty spaces in the grid now. 

 

Are date and time both taken into consideration too in the 'publish_at' command? Also, some of these Items were set on the 16th to publish on the 17th so I wonder if using the tool to publish in advance messes anything up?

Lixon_Louis
Shopify Partner
1193 35 268

@thevault  product.published_at - Returns a timestamp for when a product was published on a store.

 

https://shopify.dev/docs/themes/liquid/reference/objects/product#product-published_at

thevault
Excursionist
12 0 10

good to know, but the code you sent me still didn't work. It left all sorts of blank spaces in the grid where items should be, but weren't showing, and wasn't in descending order. I'm not 100% what the order actually was..but the more I think it over, it seems like descending order was used to decide which items show on which page of items (I have 16 pages in that collection), but then within each page the items were definitely organized in ascending order (on the example site you sent the items seem to be in ascending order too). Any ideas on how to fix the display order and blank holes? Really need to get this going without adding an app that will charge me another monthly fee.

Lixon_Louis
Shopify Partner
1193 35 268

@thevault the code i shared is an example to show the logic.  Without knowing what code or which template you are referring to (or at-least a URL) , it will create issues when you copy paste.

thevault
Excursionist
12 0 10

Debut theme and here is one of our collections we need sorted by publish date (though we really need them all to be) https://www.thevaultcollective.com/collections/swift-faire

 

thanks for the help!

 

 

 

kpvw
Shopify Partner
96 3 19

Hey there! You can try using our app Advanced Collection - https://apps.shopify.com/advanced-custom-collections - it supports sorting single or all collections by publish date on the backend - there is a free trial of 7 days. Usually sorting on the frontend i.e. via theme code can lead to issues with pagination. Hope this helps!

NattyIce311
Excursionist
32 0 4

Were you able to resolve this? I just migrated an existing store to shopify and all of our products now have the exact same date created and date published, and there's no way to edit the created or publish date to be back dated in order to fix our sorting from newest to oldest. It's a HUGE nightmare for us and something that's important for a lot of people when browsing our store of over 10,000 items 😞 

kpvw
Shopify Partner
96 3 19

Hey @NattyIce311, while you can't change the created date, you can set the published date to 'now' by setting the product to draft, and then publishing it. Assuming that you are going to curate e.g. the first 100 products, you can do that starting from the product you want to be sorted for the 100th position in the sort.

You can also set publish date to the future by going into the product > Product Status > Show More > Schedule Availability, this way you have a little bit more control, but it means that you do have to wait a while for the products to appear as published.

 

bredowmax
Shopify Partner
86 1 130

@kpvw that's unfortunately not answering the question that was asked.

 

The problem is that Shopify only allows to sort by CREATION date. But creation date cannot be changed after it has been created.

 

Merchant wants now to SORT by publishing date, because s/he has more control over publishing date. For example: an out-of-season product now is in-store again and should be displayed on top of the list.

 

The question was not "how to edit the publish date"

kpvw
Shopify Partner
96 3 19

@bredowmax actually I am answering to @NattyIce311's query about the inability to change created / published date, you can take a look at the previous post.

As for sorting by publishing date, we do have an app Advanced Collections to help with that - https://apps.shopify.com/advanced-custom-collections - as mentioned in our previous post in this thread.

Hope this helps clarify things.

bredowmax
Shopify Partner
86 1 130
Ah, sorry in that case.

While the circumstance is annoying, I solved it myself through Mechanic,
which I already use for other tasks.
NattyIce311
Excursionist
32 0 4
What is Mechanic?

Itai-Koren
Shopify Partner
31 1 35

If anyone still looking for a solution, there's no need for an app. Keep reading.

Here is a piece of code that will prepare the collection's products in the correct order by publish date instead of creation.
This will work whenever someone will change the `sort_by=` parameter to either `created_ascending` or `created_descending`, or when these options are selected as the default sorting option for the collection in the admin panel.

{% if collection.sort_by == 'created-descending' or collection.sort_by == empty and collection.default_sort_by == 'created-descending' %}
    {% assign collection_products = collection.products | sort: 'published_at' | reverse %}	
{% elsif collection.sort_by == 'created-ascending  ' or collection.sort_by == empty and collection.default_sort_by == 'created-ascending' %}
    {% assign collection_products = collection.products | sort: 'published_at' %}	        
{% else %}
    {% assign collection_products = collection.products %}	        
{% endif %}

 

Then make sure to use the newly assigned variable in the for loop, i.e. 

{% for product in collection_products %}

 

Hope this helps someone.
Will finish with the usual rant to Shopify - maybe you should listen to your valued customers and add features that are trivial to any normal ecommerce platform.

Cheers!

Need help handling the tech while you drive brand growth? DM me!
MJ_UK
Shopify Partner
1 0 1

Hi would just like to say thank you, this is brilliant.

 

Note to anyone else implementing this (with apologies if I'm stating the obvious)...

You need to find a line in your code like

{%- for product in collection.products -%}

and insert the code snippet immediately above it. For me this is in collection.liquid. Then change collection.products -> collection_products as described above. 

 

And be aware that after this your products will be ordered differently on the front end and back end of the site. 

shadowsfall118
Shopify Partner
125 13 35

@Itai-Koren I tried your suggestion and it seemed to work for the most part.

 

My problem now is I have one product that I had to separate from a variant to make it a solo product. When I deleted the variant product and re-imported the solo product shopify thinks it's new (This is expected due to the created date). I tried adding your code and updated the "published _at" date but it's making no difference in the sort order. Any ideas?

Itai-Koren
Shopify Partner
31 1 35

No difference what so ever? So what do you mean by "work for the most part"?
Just trying to understand the scope of your problem...

Are you positive that the `published_at` was updated to the expected timestamp?
If you can confirm that, then the only other explanation I can think of is that you have more than 1000 products in your store. Is this the case?

Sharing the actual URL of that collection will help a lot too.

Cheers!

Need help handling the tech while you drive brand growth? DM me!
shadowsfall118
Shopify Partner
125 13 35

@Itai-Koren I can see it did do something but when I updated a products publish date to an older date it didn't move the position.

 

For example, the product ahead of it has a date of published_at: "2018-03-27T09:25:18-04:00"

The problem product has a date of published_at: "2013-01-01T00:00:01-05:00" and the product after that one has a date of published_at: "2020-09-10T12:28:26-04:00".

 

The order is all messed up and it looks like its grabbing the sort date from somewhere else.

 

I will DM you the shop info.

Sumana_Bhattach
Shopify Partner
3 0 0

@Itai-Koren I have tried your solution. But for some reason, in the first page it does not show the latest products. It starts showing from the second page. And this happens only for 1 category. Not sure if some other apps are trying to push these products up. I have also printed the publish date in case you wanted to see it. It seems we are pretty close....

 

https://0gl57u4h3a9cphun-14697362.shopifypreview.com/collections/buy-silk-mark-certified-saree

Michi2
Shopify Partner
9 0 4

Sorry for bringing up this old topic.

 

@Itai-Koren  While I appreciate your approach it doesn't seem to work with pagination at all. It seems to sort the products only within a single page because pagination still acts on collection.products. If I try to paginate on the new collection_products array Shopify tells me that pagination on arrays like these isn't supported.

Itai-Koren
Shopify Partner
31 1 35

Hey Michi, no worries, it's good to refresh the memory once in a while 😉
Did you try wrapping my code with the following?
As long as you have less than 1000 products on your store - it should works AFAIK:

{% paginate all_products by 50 %}
    ... CODE GOES HERE ...
{% endpaginate %}

 

Let me know if that works, and if it does I'll try to edit my original post so it's clearer.

Cheers!

Need help handling the tech while you drive brand growth? DM me!
Janvi149
Shopify Partner
1 0 0

I have tried your solution, But it is not working on pagination.We have more than 1000 products in one collection and I already tried your last suggestion.

shadowsfall118
Shopify Partner
125 13 35

@Janvi149 you have to remove pagination for this solution to work. Because of how Shopify pulls the products for each page it will only sort each pagination page. You can try removing pagination and see how much it decreases your shop performance. Shopify still lazy loads the images so in my case I just removed the pagination to be able to sort the products properly. 

 

I really think Shopify devs need to integrate custom sorting by custom meta fields as when we did our shop we moved from another site so the sort order was determined in the order of which products where imported and did not realize this until later down the line.

 

Hope that helps.

webcase_Design
Shopify Partner
4 0 0

I am having the same issue trying to sort my catalog of over 1k products. Have you found a solution yet?