Forum Discussion

bhupen's avatar
bhupen
Advisor
11 years ago
Solved

How to show only 5 posts?

Hi guys,

 

I need to show only 5 posts. Somehow I am able to show the latest posts but My query is not working to show up only 5 posts:

Following is the code I have done:

 

<#list restadmin("/roles/name/Administrator/users").users.user as user>  <#-- REST call to get the user's roles -->
<#list restadmin("/users/id/${user.id}/posts/latest?page_size=5").messages.message as recent>
<a href="${recent.@view_href}">${recent.subject}</a></br>
</#list>
</#list>

 

Out put for the above code:

Its showing more then 8 posts at front end

Can any one tell me how can I show only 5 posts?

  • bhupen 

     

    You can mix API v1 and API v2 calls.

    If you want to optimize the code, have a look at the User resource in the API v2 documentation and check if you can get the favourite.

     

    Here is the code that should work. You need to provide a valide user.id for your rest call.

    It will loop the user favourite and build a list for the WHERE CLAUSE used to get the last 5 messages.

     

    <h3>Latest Posts from Favorites Users</h3> <br />
    <#assign whereclause = ""/>
    <#list restadmin("/users/id/${user.id}/addressbook/contacts/friends").users.user as user>
    <#if user??>
    <#assign whereclause = whereclause + "'" + user.id + "'" />
    <#if user_has_next><#assign whereclause = whereclause + "," /></#if>
    </#if>
    </#list>

    <#if wherclause??>
    <#assign messages = rest("2.0","/search?q=" + "SELECT subject, view_href, post_time_friendly, board.id, author.id FROM messages WHERE author.id IN (${whereclause}) ORDER BY post_time DESC LIMIT 5"?url) />


    <#list messages.data.items as recent >
    <#if recent??>
    <a href="${recent.view_href}">${recent.subject}</a><br/>
    </#if>
    </#list>
    </#if>

     

     

     

     

     

     

26 Replies

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)
    11 years ago

    bhupen 

     

    Ok, so, this should work ...

    Sorry, this is using API v2 and not v1.

     

    First, you get the list of your Administrators id.

    Then you build a list of those id to use in a WHERE IN clause.

    Finally, you get the last 5 messages (by using ORDER BY DESC and LIMIT 5)

     

    <h3>Latest Posts from Administrator</h3> <br />

    <#assign users = rest("2.0","/search?q=" + "SELECT id, login FROM users WHERE roles.name='Administrator'"?url) />

    <#if users??>
    <#assign whereclause = ""/>
    <#list users.data.items as user>
    <#assign whereclause = whereclause + "'" + user.id + "'" />
    <#if user_has_next><#assign whereclause = whereclause + "," /></#if>
    </#list>
    </#if>

     

    <#assign messages = rest("2.0","/search?q=" + "SELECT subject, view_href, post_time_friendly, board.id, author.id FROM messages WHERE author.id IN (${whereclause}) ORDER BY post_time DESC LIMIT 5"?url) />

     

    <#list messages.data.items as recent >

    <#if recent??>
    <a href="${recent.view_href}">${recent.subject}</a><br/>
    </#if>

    </#list>

  • bhupen's avatar
    bhupen
    Advisor
    11 years ago

    OlivierS  This seems nice approach to me and it works for me. I have same kind of other requiement,

    Requrement:

     

    I want to show the posts from the "Favourtie users" only. I have done it using restapi but not sure how can I Implement it using API v2.

    rest API i have used:

    <#list restadmin("/users/id/${user.id}/addressbook/contacts/friends").users.user as user>

     

    at the previous requirement I saw you you put the where clause in  "roles.name='Administrator". So how can I do the same for favourite user post.

     

    Your help is really appreciated

     

    Regards,

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)
    11 years ago

    bhupen 

     

    You can mix API v1 and API v2 calls.

    If you want to optimize the code, have a look at the User resource in the API v2 documentation and check if you can get the favourite.

     

    Here is the code that should work. You need to provide a valide user.id for your rest call.

    It will loop the user favourite and build a list for the WHERE CLAUSE used to get the last 5 messages.

     

    <h3>Latest Posts from Favorites Users</h3> <br />
    <#assign whereclause = ""/>
    <#list restadmin("/users/id/${user.id}/addressbook/contacts/friends").users.user as user>
    <#if user??>
    <#assign whereclause = whereclause + "'" + user.id + "'" />
    <#if user_has_next><#assign whereclause = whereclause + "," /></#if>
    </#if>
    </#list>

    <#if wherclause??>
    <#assign messages = rest("2.0","/search?q=" + "SELECT subject, view_href, post_time_friendly, board.id, author.id FROM messages WHERE author.id IN (${whereclause}) ORDER BY post_time DESC LIMIT 5"?url) />


    <#list messages.data.items as recent >
    <#if recent??>
    <a href="${recent.view_href}">${recent.subject}</a><br/>
    </#if>
    </#list>
    </#if>

     

     

     

     

     

     

  • bhupen's avatar
    bhupen
    Advisor
    11 years ago

    OlivierS  Thanks a ton. You are really a great help here. there was some typo error. Iam just pasting code again after correcting, if someone else  need this code he can refer this. you just mistype "whereclause "

     

    <h3>Latest Posts from Favorites Users</h3> <br />
    <#assign whereclause = ""/>
    <#list restadmin("/users/id/${user.id}/addressbook/contacts/friends").users.user as user>
    <#if user??>
    <#assign whereclause = whereclause + "'" + user.id + "'" />
    <#if user_has_next><#assign whereclause = whereclause + "," /></#if>
    </#if>
    </#list>

    <#if wherclause??>
    <#assign messages = rest("2.0","/search?q=" + "SELECT subject, view_href, post_time_friendly, board.id, author.id FROM messages WHERE author.id IN (${whereclause}) ORDER BY post_time DESC LIMIT 5"?url) />


    <#list messages.data.items as recent >
    <#if recent??>
    <a href="${recent.view_href}">${recent.subject}</a><br/>
    </#if>
    </#list>
    </#if>

     

    Rest of code works fine for me.

    Regards

  • INTANK1's avatar
    INTANK1
    Adept
    9 years ago

    This helpful to implement in many list condition.