Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order entries by rating and paginate #6

Open
marksimons opened this issue Dec 10, 2018 · 2 comments
Open

Order entries by rating and paginate #6

marksimons opened this issue Dec 10, 2018 · 2 comments

Comments

@marksimons
Copy link

Is it possible to get all entries from a section and then order them by the rating? The end goal is to have a dropdown where users can select to view only 3 star rated content, or only 5 star rated content etc..

My code at the moment returns the entries but I am unsure of how to sort them by rating.

{% set params = { section: 'features', limit: 11, relatedTo: category } %} 
{% paginate craft.entries(params) as pageInfo, newsEntries %}	
{% for entry in newsEntries %}    
    {% set ratingValue = entry.ourScore %}		
    <h3>{{ entry.title }}</h3>
    Rating: {{ ratingValue }}
{% endfor %}

// pagination logic below
<p class="pagination">		
<a href="{{ pageInfo.firstUrl }}">First</a>
{% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}">Prev</a>{% endif %}
{% for page, url in pageInfo.getPrevUrls(2) %}
<a href="{{ url }}">{{ page }}</a>
{% endfor %}
<span class="current">{{ pageInfo.currentPage }}</span>
{% for page, url in pageInfo.getNextUrls(2) %}
<a href="{{ url }}">{{ page }}</a>
{% endfor %}
{% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}">Next</a>{% endif %}
<a href="{{ pageInfo.lastUrl }}">Last</a>
</p>
@lindseydiloreto
Copy link
Contributor

Hi Mark, here's the full documentation for sorting by highest rated...

https://www.doublesecretagency.com/plugins/star-ratings/docs/sort-by-highest-rated

In your case, you're looking for something like this...

{% set params = { section: 'features', limit: 11, relatedTo: category } %}
{% set allNewsItems = craft.entries(params) %}

{% do craft.starRatings.sort(allNewsItems) %}

{% paginate allNewsItems as pageInfo, newsEntries %}	
{% for entry in newsEntries %}

Hope that helps!

@marksimons
Copy link
Author

Hi, thanks for getting back to me.

I may be confused by your example but how could I adapt that to display entries by certain rating criteria. I don’t simoly want to display the highest rated content.

For example From a drop down that a user can select “view 3 star rated content only” in which case I would display content that has a rating of 3 stars. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants