I ended up just using MYSQL instead of get_records()
.
Here’s my current version in case anyone finds this later when trying to solve a similar problem.
function scopedNavigation($item=null, $collectionid=null, $typeid=null){
$html = null;
$current_item_id = $item->id;
$appendParam = null;
$column = null;
$value = null;
$itemsArray=array();
if($current_item_id){
// limit by collection OR type (not both)
if(strlen($collectionid) > 0){
$appendParam = '?collection='.$collectionid;
$column = 'collection_id';
$value = $collectionid;
}elseif(strlen($typeid) > 0){
$appendParam = '?type='.$typeid;
$column = 'item_type_id';
$value = $typeid;
}
// db query
$db = get_db();
$prefix=$db->prefix;
if($column && $value){
$select = "SELECT id FROM `".$prefix."items` WHERE `".$column."` = ".$value." AND `public` = 1";
}else{
$select = "SELECT id FROM `".$prefix."items` WHERE `public` = 1";
}
$sql = $select;
$q = $db->query($sql);
$results = $q->fetchAll();
// build an array of item ids
foreach($results as $result){
$itemsArray[] = $result['id'];
}
// find the ids on either side of the current item id
if(count($itemsArray)){
$index = array_search($current_item_id, $itemsArray);
if($index !== false && $index > 0 ){
$prev = $itemsArray[$index-1];
$link_previous = $prev ? '<a class="scoped" href="'.url('items/show/'.$prev.$appendParam).'">'.__('Previous Item').'</a>' : null;
}
if($index !== false && $index < count($itemsArray)-1){
$next = $itemsArray[$index+1];
$link_next = $next ? '<a class="scoped" href="'.url('items/show/'.$next.$appendParam).'">'.__('Next Item').'</a>' : null;
}
}
}
$html .= '<nav>';
$html .= '<ul class="item-pagination">';
$html .= '<li id="previous" class="previous">'.$link_previous.'</li>';
$html .= '<li id="next" class="next">'.$link_next.'</li>';
$html .= '</ul>';
$html .= '</nav>';
return $html;
}
Note that it may or may not be more performant to just get the next and previous ids by doing two separate database queries (example), but I found this current version to be good enough for my needs for now.