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.
@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 %}
Oh I am not looking to sort my list of collectionsā¦Iām looking to sort a collectionās products by their publish dates
@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%}
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
@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.
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?
@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
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.
@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.
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!
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!
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 ![]()
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.
@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ā
@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.
Ah, sorry in that case.
While the circumstance is annoying, I solved it myself through Mechanic,
which I already use for other tasks.
What is Mechanic?
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.
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.