Copied to clipboard

Flag this post as spam?

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


  • Troels Johnsen 4 posts 24 karma points
    Jun 02, 2021 @ 18:36
    Troels Johnsen
    0

    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.

    1. How do I add tags to a content type?

    2. 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?

    3. 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.

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jun 02, 2021 @ 18:42
    Søren Gregersen
    0
    1. there is an built-in editor for tags - https://our.umbraco.com/Documentation/Fundamentals/Backoffice/Property-Editors/Built-in-Property-Editors/Tags/

    2. 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).

    3. 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

  • Troels Johnsen 4 posts 24 karma points
    Jun 02, 2021 @ 18:52
    Troels Johnsen
    0

    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

    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?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jun 02, 2021 @ 18:58
    Søren Gregersen
    0

    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);
    
  • Troels Johnsen 4 posts 24 karma points
    Jun 02, 2021 @ 19:11
    Troels Johnsen
    0

    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?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jun 02, 2021 @ 19:14
    Søren Gregersen
    0

    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.

  • Troels Johnsen 4 posts 24 karma points
    Jun 02, 2021 @ 19:26
    Troels Johnsen
    0

    I am at 20.000 articles 😱

    I think I have to look into examine :-)

Please Sign in or register to post replies

Write your reply to:

Draft