Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Plugin: bb-Topic-Views


_ck_
Participant

@_ck_

Here’s a plugin to extend the topic-views plugin and add a “most-views” view page (ie. view.php?view=most-views)

It does it with a minimum number of mysql calls, just 10 to display an entire page on my setup. You have to hack your views.php template to add the views column, use $topic->views for the view count to use the cached data in memory instead of an extra mysql query for each topic.

function most_views_views( $views ) {
global $views;
$views['most-views'] = 'Topics with the most views';
return $views;
}
add_filter('bb_views', 'most_views_views');

function most_views( $view ) {
global $bbdb, $topics, $view_count;
switch ( $view ) :
case 'most-views' :
$limit = bb_get_option('page_topics');
$where = apply_filters('get_latest_topics_where','');
$most_views = $bbdb->get_results("SELECT topic_id FROM $bbdb->topicmeta WHERE meta_key='views' ORDER BY cast(meta_value as UNSIGNED) DESC LIMIT $limit");
foreach (array_keys($most_views) as $i) {$trans[$most_views[$i]->topic_id] =& $most_views[$i];} $ids = join(',', array_keys($trans));
$topics ="SELECT * FROM $bbdb->topics WHERE topic_id IN ($ids) $where ORDER BY FIELD(topic_id, $ids)";
$topics = $bbdb->get_results($topics);
$view_count = count($topics);
$topics = bb_append_meta( $topics, 'topic' );
break;
default :
do_action( 'bb_custom_view', $view );
endswitch;
}
add_action( 'bb_custom_view', 'most_views' );

Skip to toolbar