Forum

Thread tagged as: Question, Problem, Field-Types

Sorting Query Results with perch_content_custom()

I'm having difficulty using perch_content_custom() and sorting the results by date added. I want my results to be DESC, yet can't quite figure out how to do this.

Here's the block I'm using

perch_content_custom('Projects', array(
     'template' => '_project_listing.html',
     'filter'   => 'project_type',
     'match'    => 'eq',
     'value'    => 'current',
     'count'    => 2
));

I've changed it to

perch_content_custom('Projects', array(
     'template'   => '_project_listing.html',
     'filter'     => 'project_type',
     'sort-order' => 'DESC',
     'match'      => 'eq',
     'value'      => 'current',
     'count'      => 2
));

And the result is nothing. Blank. I've also tried incorporating 'sort => 'itemID', and played around with multiple values; still empty results.

Here's the query that is output using PerchUtil::output_debug();

SELECT * FROM ( SELECT idx.itemID, c.regionID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx 
JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID
JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID='1' AND idx.itemRev='160')) AND ((idx.indexKey='project_type' AND idx.indexValue='current')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 2

Changing ORDER BY sortval ASC to ORDER BY sortval DESC gives me the result I want. How can I change this?

Tim Gavin

Tim Gavin 0 points

  • 6 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

sort-order needs sort with the ID of the field you want to sort on. That should be a field in your template.

https://docs.grabaperch.com/docs/content/perch-content-custom/

OK, thanks. That fixed it.

The doc text "The ID of the field to sort on" is really ambiguous. I was thinking database ID, not template ID. :)