Copied to clipboard

Flag this post as spam?

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


  • Andrew Cullen 131 posts 221 karma points
    May 17, 2017 @ 20:38
    Andrew Cullen
    0

    TagQuery and custom fields

    I am working on a "related content" widget that shows the 3 latest articles with the same tags, with "latest" being determined by a custom field named "articleDate". I can't seem to figure out how to use custom fields in a query of the returned published content. Below is my attempt:

    var publishedContent = Umbraco.TagQuery.GetContentByTag(tag);

        if (publishedContent.Where(p=>p.Id!=CurrentPage.Id).Count() > 0)
            {
    @foreach (var item in publishedContent.Where(p=>p.Id!=CurrentPage.Id).OrderByDescending(p => p.articleDate).Take(3)) {
                            var page = Umbraco.Content(item.Id);
                            <li class="list-group-item">
                                <a href="@item.Url">@item.Name
                                <div class="articleDate">@page.articleDate.ToString("MMMM dd, yyyy")</div></a>
                            </li>
                        }
    }
    
  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    May 18, 2017 @ 13:07
    Alex Skrypnyk
    100

    Hi Andrew

    I would like to rewrite this code:

    var publishedContent = Umbraco.TagQuery.GetContentByTag("tag").ToList();
    
    if (publishedContent.Any(p => p.Id != Umbraco.AssignedContentItem.Id))
    {
        foreach (var item in publishedContent.Where(p => p.Id != Umbraco.AssignedContentItem.Id).OrderByDescending(p => p.GetPropertyValue<DateTime>("articleDate")).Take(3))
        {
            var page = Umbraco.Content(item.Id);
    
            <li class="list-group-item">
                <a href="@item.Url">
                    @item.Name
                    <div class="articleDate">@page.articleDate.ToString("MMMM dd, yyyy")</div>
                </a>
            </li>
        }
    }
    
  • Andrew Cullen 131 posts 221 karma points
    May 18, 2017 @ 14:15
    Andrew Cullen
    0

    Thanks, Alex!

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    May 18, 2017 @ 15:24
    Alex Skrypnyk
    0

    You are welcome!!!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies