Copied to clipboard

Flag this post as spam?

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


  • Mike Manusama 45 posts 195 karma points
    Feb 15, 2022 @ 16:46
    Mike Manusama
    0

    Query posts by document tag

    I am using the out of the box Umbraco query builder to query posts that I have created. The query builder makes it very easy to query posts by creation date or updated date, but if i wanted to make a "featured post" section, how would I go about that? All of my blog posts have a tag section in their document type. In addition to relative keyword tags, I would also like to tag anything that I want to be "featured".

    Ultimately I would like to pull in the latest 1-3 featured articles so they remain sticky at the top of my blog page. Some of these were created months ago so they don't get seen with the standard .CreateDate function. Any help would be appreciated. Thanks!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 20, 2022 @ 02:11
    Dennis Aaen
    0

    Hi Mike,

    What a great question you have here.

    To show posts with a specific tag, you can use TagQuery and simple URL-based filtering, so when a user visits your-domain/blog?tag=featured only posts tagged with “featured” will be listed:

    On the template that you use to list all the posts, you should be able to do something like this

    var tag = Request.QueryString["tag"];
    var articles =  (tag != null)
                    ? Umbraco.TagQuery.GetContentByTag(tag).OfType<NewsArticle>()
                    : Model
                       .Children<NewsArticle>()
                       .OrderByDescending(x => x.CreateDate).Take(3);
    

    Remember you will need to replace bits and pieces so they match your document types and so on

    Hope this can give you an idea how you can do it

    Best

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft