I need to show the number of times a user has favorited a specified set of entries. I've tried many ways, including different combinations of using Stash and Mx_calc, but have been unsuccessful.

The exp:favorites:count tag doesn't seem to allow this without help. I would like it to do something like {exp:favorites:count entry_id="130|132|463" favorites_member_id="CURRENT_USER"}, but this is not allowed.

I've tried setting the numbers of each entry with Stash and then adding them with Mx_calc but can't get that to work (below). Maybe there's a simpler way?

{exp:stash:set name="total-favorites" parse_tags="yes"}
  {exp:channel:entries channel="topic-content" search:topic-title="advertising" category="5" search:topic-view="IS_EMPTY" dynamic="no" parse="inward"}  
    {exp:favorites:count entry_id="{entry_id}" favorites_member_id="CURRENT_USER"}

  {exp:mx_calc parse="inward" expression="sum({exp:stash:get:blah name="total-favorites"})"}
{exp:stash:parse trim="yes" parse="inward"}
  {exp:mx_calc parse="inward" expression="sum({exp:stash:get:blah name="total-favorites"})"}
  • What's your use case? Are you looping thru chanmel entries whilst outputting the logged in users likes or is this a separate call elsewhere on the page? – stuartmcd69 Mar 14 '16 at 7:56
  • Not 100% sure of your question. Everything is included in the code above. Yes I am looping through the channel entries, and using the outputted entry id's to determine whether the logged in user has in fact voted (favorited) on each of those entries. I just need to get the total number of times the user has voted on all of those entries combined. (Getting the count for each entry id is no problem - getting the sum of each one according to the channel:entries loop is what is confusing me.) – Geoff Mar 14 '16 at 20:30

Well to start with you're capturing a single number, by adding stash around the EE channels tags, it will only ever capture/hold the last number or 0.

To hold all numbers you should use something like the following (NOTE: This is a theory and not verbatim code, refer the documents and experiment) I've done similar but not with Favorites so cannot confirm code of usage:

{exp:channel:entries channel="topic-content" search:topic-title="advertising" category="5" search:topic-view="IS_EMPTY" dynamic="no" parse="inward"}  
    {exp:stash:append name="fav_count" scope="user" trim="yes"}{exp:favorites:count entry_id="{entry_id}" favorites_member_id="CURRENT_USER"}+{/exp:stash:append}
//NEW - Add bogus 0 to end of stash
{exp:stash:append name="fav_count" scope="user" trim="yes"}0{/exp:stash:append}

What this does is append the result of the favourites tags, trims any whitespace which would kill the sum action (next), adds a scope to the current user as well as adding the "+" symbol, which we can now utlise like this:

  {exp:mx_calc parse="inward" expression="sum({exp:stash:get name='fav_count' scope='user'})"}

Assuming the stash capture results look like this: 0+1+2+0+ the sum would render like exp:mx_calc parse="inward" expression="sum(0+1+2+0+)

Failing that (or variations). Wrap each result in a unique class and use Javascript to find, record, add up and inject into a result tag.

UPDATE: Added a trailing '0' to the original capture (the trailing '+' was borking the math module. Inside the math function, used single quotes rather then doubles.


{exp:channel:entries channel="topic-content" search:topic-title="advertising" category="5" search:topic-view="IS_EMPTY" dynamic="no" parse="inward" }  
  {exp:stash:append name="fav_count" parse_tags="yes" trim="yes" parse_conditionals="yes"}
    {exp:favorites:count entry_id="{entry_id}" favorites_member_id="CURRENT_USER"}
    {if "{count}" != "{total_results}"}+{/if}

{exp:stash:parse process="end"}
{exp:mx_calc expression="sum({stash:fav_count})"} 
  • Thanks for the suggestions. Using exp:stash:append and the "+" seems like the right way to go. It turns out that process=“end” is also needed. I am testing the results with {exp:stash:get name="fav_count" process="end" trim="yes" scope="user”} but instead of returning the numbers to be added there is a string like M00o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr+. I have checked the documentation and online help and have tried various additional tags like parse=“inward” but haven’t found anything yet to get this to work properly. – Geoff Mar 15 '16 at 16:57
  • Updated my original answer. PS. You never need to add trim='yes' to the get call - it's a setter param. – stuartmcd69 Mar 16 '16 at 2:32
  • Wow, I finally got it! Thanks for your help in guiding towards this. I'm not sure why, but the append code started to work properly and I was able to use the plus sign at the end of the looped entries. I also had to use a conditional to remove the final "+". For the mx_calc code, I found some instructions on using the simpler version pasted above – Geoff Mar 16 '16 at 5:27
  • Nice one, glad to see you got it working and picked up a little on the way :) – stuartmcd69 Mar 16 '16 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.