Take 5 latest articles by tag and sorted på Published Date
Hi,
I am a complete Umbraco noob, so bear with me if my question doesn't make sense :-)
I would like to pull out a list of the 5 latest published articles with a certain tag. How would I do that?
So, I have a few questions.
How do I add tags to a content type?
I don't see a a PublishDate on the content model, but only CreateDate and UpdateDate. How do I get a date that is set when the article is Published and won't change if the content is updated?
How do I query the 5 latest articles with a specific set of tags ordered by PublishDate in a descending order (newest first)
This seems like a fairly simple use case, but I can't seem to figure out how to achieve this with Umbraco.
Normally you just add the publishdate. Then you could filter your lists, to only show news where publishdate < now, and maybe add a check to return a 404 if the page is not published (remember to check for preview etc).
var taggedContent = Umbraco.TagQuery.GetContentByTag("News");
But I can't really seem to find anything about how to limit and sort the query? How would I limit the query to only the latest X articles ordered by PublishDate?
You could just add .OrderBy(x => x.Value<DateTime>("PublishDate")) .Take(5)
This orders the result by the PublishDate (that you need to add), and then takes for first 5.
When you use modelbuilder (https://our.umbraco.com/Documentation/Reference/Templating/Modelsbuilder/) you could also cast the results to your type by using .OfType<YourDocType>().OrderBy(x => x.PublishDate) .Take(5)
var taggedContent = Umbraco.TagQuery.GetContentByTag("News").OfType<YourDocType>().OrderBy(x => x.PublishDate) .Take(5);
Great! Thank you very much! It seems just like what I need.
I can see that these methods are on a collection, so I was just wondering how this scales with a lot of content. Lets say I have an archive of 20.000 articles.
Will this function fetch all the content from the database and then sort and limit the results or are this done at the database layer, so only the relevant content is added to the collection?
Take 5 latest articles by tag and sorted på Published Date
Hi,
I am a complete Umbraco noob, so bear with me if my question doesn't make sense :-)
I would like to pull out a list of the 5 latest published articles with a certain tag. How would I do that?
So, I have a few questions.
How do I add tags to a content type?
I don't see a a PublishDate on the content model, but only CreateDate and UpdateDate. How do I get a date that is set when the article is Published and won't change if the content is updated?
How do I query the 5 latest articles with a specific set of tags ordered by PublishDate in a descending order (newest first)
This seems like a fairly simple use case, but I can't seem to figure out how to achieve this with Umbraco.
I hope it makes sense and that you can help me.
there is an built-in editor for tags - https://our.umbraco.com/Documentation/Fundamentals/Backoffice/Property-Editors/Built-in-Property-Editors/Tags/
Normally you just add the publishdate. Then you could filter your lists, to only show news where publishdate < now, and maybe add a check to return a 404 if the page is not published (remember to check for preview etc).
There is a lot of ways to do this, but a good start would be using the built-in methods documented here https://our.umbraco.com/Documentation/Reference/Querying/UmbracoHelper/#working-with-tags
Hi Søren
Thank you for the reply.
I just looked at https://our.umbraco.com/Documentation/Reference/Querying/UmbracoHelper/#working-with-tags
It seems like I can get tagged content by using
But I can't really seem to find anything about how to limit and sort the query? How would I limit the query to only the latest X articles ordered by PublishDate?
You could just add
.OrderBy(x => x.Value<DateTime>("PublishDate")) .Take(5)
This orders the result by the PublishDate (that you need to add), and then takes for first 5.
When you use modelbuilder (https://our.umbraco.com/Documentation/Reference/Templating/Modelsbuilder/) you could also cast the results to your type by using
.OfType<YourDocType>().OrderBy(x => x.PublishDate) .Take(5)
Great! Thank you very much! It seems just like what I need.
I can see that these methods are on a collection, so I was just wondering how this scales with a lot of content. Lets say I have an archive of 20.000 articles.
Will this function fetch all the content from the database and then sort and limit the results or are this done at the database layer, so only the relevant content is added to the collection?
Say you get to 20000 articles, you will have other problems too ;-)
But yes, it fetches all items (from cache), and does the query on them all.
You could look into examine later on to get more performance.
I am at 20.000 articles 😱
I think I have to look into examine :-)
is working on a reply...