doublesecretagency / craft-starratings

Star Ratings plugin for Craft CMS
Other
4 stars 3 forks source link

Order entries by rating and paginate #6

Open marksimons opened 5 years ago

marksimons commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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.