diff --git a/weblinks.module b/weblinks.module
index 6593121..8265313 100644
--- a/weblinks.module
+++ b/weblinks.module
@@ -1165,6 +1165,10 @@ function _weblinks_get_query($tid = 0, $sort = 'title', $limit = 0) {
       break;
     case 'popular':
       $cols = 'DISTINCT(n.nid), bw.click_count, bw.last_click';
+      if (is_numeric($tid)) {
+        $join .= $join_tn;
+        $wheres[] = $where_tn;
+      }
       $wheres[] = 'bw.click_count>0';
       $order = 'ORDER BY bw.click_count DESC, bw.last_click DESC';
       break;
@@ -1176,7 +1180,7 @@ function _weblinks_get_query($tid = 0, $sort = 'title', $limit = 0) {
       break;
     case 'recent':
       $cols = 'DISTINCT(n.nid), n.changed, n.title';
-      if ($tid != 0) {
+      if (is_numeric($tid) && $tid != 0) {
         $join .= $join_tn;
         $wheres[] = $where_tn;
       }
