I have a bit of code, it works to get recent posts from Discuss forums in MODX.
<?php
$discuss = $modx->getService('discuss','Discuss',$modx->getOption('discuss.core_path',null,$modx->getOption('core_path').'components/discuss/').'model/discuss/');
if (!($discuss instanceof Discuss)) return true;
$c = $modx->newQuery('disPost');
$c->limit(10);
$c->sortby('createdon', 'DESC');
$posts = $modx->getCollection('disPost', $c);
foreach ($posts as $post) {
$temp = $post->toArray();
$temp['url'] = $post->getUrl();
// call chunks or what you want
$out[] = $modx->getChunk('rowTpl', $temp);
}
return implode("\n", $out);
It works, however its imperfect. For instance, I want to filter out private messages, and at the post level this isn't working, there is no private flag at the post level.
So what I need to do is to grab the threads, sort private or no, and then take that data and find posts by createdon and sort DESC, so basically another routine prior to the db call in this one.
Anyone have any ideas? Any help would be appreciated!
Take a look at the database model, if there is a field in another table that flags posts as private, you should be able to filter them out by using getCollectionGraph and another little parameter to your code.
I'm not familiar with the database schema, so the following code will not work [it's not even related], but gives you an idea of what you should be looking for and how to join the related tables:
if($queryFilters['status_id'] == '' ){$queryFilters['status_id'] = 2;}
$mygroups = $this->getGroupMemberships(); // an array of group ids
$criteria = $this->modx->newQuery('FundRequest');
$criteria->leftJoin('Program','Program');
$criteria->where($queryFilters); // an array('id' => 2, 'uid' => 3) etc..
$criteria->andCondition(array('Program.location_id:IN' => $mygroups));
$criteria->sortby('`FundRequest`.`request_date`','desc');
$fundRequests = $this->modx->getCollectionGraph('FundRequest', '{ "StatusFlag":{}, "PaymentMethod":{}, "Contract":{ "Client": {}, }, "Program": { "Location": {}, "ProgramName": {}, } }', $criteria);
Hopefully this gives you some clues.