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

How to sort by votes in Elements API #5

Open
arifje opened this issue Sep 10, 2018 · 2 comments
Open

How to sort by votes in Elements API #5

arifje opened this issue Sep 10, 2018 · 2 comments

Comments

@arifje
Copy link

arifje commented Sep 10, 2018

I would like to create a feed with entries sorted by votes, so I would like to know how to translate this example to PHP:

{% set entries = craft.entries.section('media').limit(15) %}
{% do craft.upvote.sort(entries) %}

{% for entry in entries %}
  //	
{% endfor %}

Thanks!

@lindseydiloreto
Copy link
Contributor

The literal translation of {% do craft.upvote.sort(entries) %} is this...

Upvote::$plugin->upvote_query->orderByTally($elements);

https://github.com/doublesecretagency/craft-upvote/blob/v2/src/variables/UpvoteVariable.php#L247

Give that a shot!

@arifje
Copy link
Author

arifje commented Dec 17, 2018

Hi, I am still looking for a way to achieve this in Elements API. Your translation didn't help much so sar, I have no clue how to implement it ;-(

This is my endpoint from Elements API, where I retreive the entries. I would like to get te latest entries sorted by votes, so not only sort the latest entries/result, if you know what I mean :-)

'app/entries' => function() {

    // mode
    $mode = \Craft::$app->request->getQueryParam('mode');
   	
	// featured
	if($mode=="featured") {
		$criteria['featured'] = true;
	}
	
	// sort by views
	if($mode=="trending") {
		$criteria['postDate'] = '>=' . date('Y-m-d', strtotime("-15 days"));
	}
						       	
    return [
        
		'serializer' => 'jsonFeed',
		'resourceKey' => 'entries',
        'elementType' => Entry::class,
        'elementsPerPage' => 15,
		'criteria' => $criteria,
        'transformer' => function(Entry $entry) {
            
			// total views
			$views = 0;
			$views = EntryCount::$plugin->entryCount->getCount($entry->id)->count;
			              
            // votes	               
            $score = Upvote::$plugin->upvote_query->tally($entry->id);
            	                
            return $entries = [  
                'timestamp' => $entry->postDate->getTimestamp(),
                'date_published' => $entry->postDate->format('d/m H:i'),
				'date_modified' => $entry->dateUpdated->format('d/m H:i'),           
                'entry_id' => $entry->id,
                'title' => $entry->title,
                'link' => $entry->url,
                'views' => $views,
                
                'votes' => [
                	'up' => 0,
                	'down' => 0,
                	'score' => $score 
                ],                     
                
            ];
          
        },
        'cache' => false,
        'paginate' => true,
		'pretty' => true
    ];
},

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