How to hide topics of multi categories and their all sub-categories from latest page

I want exclude topics of several categories and their sub categories. I read the post: https://meta.discourse.org/t/can-i-hide-a-category-from-the-latest-page/6728
but that does not work.

latest,-category1,-category2

this does not work?

1 Like

I read the code and found the it does not support this. So I just wrote some code for this. Demo: suanfazu.com

Usage:

latest,-category1:category2:subcategory1:subcategory2

Code below (i’m totally new to Ruby, this is my first ruby code):

root@suanfazu:/var/www/discourse# git diff 
diff --git a/app/models/top_menu_item.rb b/app/models/top_menu_item.rb
index 302a264..8e8d079 100644
--- a/app/models/top_menu_item.rb
+++ b/app/models/top_menu_item.rb
@@ -48,7 +48,7 @@ class TopMenuItem
   def initialize_filter(value)
     if value
       if value.start_with?('-')
-        value[1..-1] # all but the leading -
+        value[1..-1].split(':') # all but the leading -
       else
         Rails.logger.warn "WARNING: found top_menu_item with invalid filter, ignoring '#{value}'..."
         nil
diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index fe6d8e8..33fb001 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -276,7 +276,15 @@ class TopicQuery
 
       result = apply_ordering(result, options)
       result = result.listable_topics.includes(:category)
-      result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) if options[:exclude_category]
+#      result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) if options[:exclude_category]
+      if options[:exclude_category]
+          if options[:exclude_category].kind_of?(Array)
+              result = result.where('categories.name is null or categories.name not in (?)', options[:exclude_category])
+          else
+              result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category])
+          end
+          result = result.references(:categories)
+      end
 
       # Don't include the category topics if excluded
       if options[:no_definitions]
2 Likes

And looks there’s a bug here and it’s very easy to repro:

  1. on home page, click to any category to navigate to the category page
  2. click the top logo to go to home page, and you can still see the topics of excluded categories

and on IE, get an error saying /latest.json?exclude_category=category1:category2 cannot be loaded because of internal server error, but there’s no error log in admin logs.

(and when you refresh the page, the topics will be loaded correctly again)

服务器错误

无法载入 /latest.json?exclude_category=category1:category2 

错误代码:500 Internal Server Error 

返回 再试一次 

hope someone could fix this. @codinghorror

I think I found the reason: my category name is in Chinese.

This request just fails:

http://suanfazu.com/latest.json?exclude_category=some_chinese_category_name_中文

on IE i got 500 error
on Chrome the filter does not work at all

@codinghorror, please fix it, seems a bug

I just made a workaround (code below). Not sure if this may cause other problems. @codinghorror please help to fix

diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb
index bea0f7c..cb2df7a 100644
--- a/app/controllers/list_controller.rb
+++ b/app/controllers/list_controller.rb
@@ -237,7 +237,8 @@ class ListController < ApplicationController
     options = {
       page: params[:page],
       topic_ids: param_to_integer_list(:topic_ids),
-      exclude_category: (params[:exclude_category] || select_menu_item.try(:filter)),
+      # exclude_category: (params[:exclude_category] || select_menu_item.try(:filter)),
+      exclude_category: (select_menu_item.try(:filter) || params[:exclude_category]),
       category: params[:category],

hey!! I would like to have the exact feature and I’m working with Chinese now. Any way to do this?

Today there is an option in the category settings dialog to hide from latest.

3 Likes

really? is it new? Where is it? Can you post a picture?

This is the setting page of one of my categories

If latest is set as your homepage, you want to use Suppress this category from the homepage

2 Likes

Thanks for you reply!!

I still want this category in the category list, not suppressing it, just not in the latest page. Is it possible?

What is set as your homepage? If it is Latest, that is exactly what that setting should do.

no, the homepage is category. and I select categories with latest topics

now it’s like this

I like it, just I want to hide certain categories from the right column

sorry for the Chinese

2 Likes

Unfortunately, it doesn’t seem to do that, it removes it from the left side, not the right side (which leaves this as a feature request).

Hey I suddenly find a setting “mute category”. Does this help?

what does this mean? all topics in a category will be muted and muted topics will not be shown in the latest? I added those categories I would live to hide from LATEST, but it doesn’t work. How can I tag a topic with muted tag?

I fnd it a bit odd that on the newer hybrid category page, the tags have an empty class value.

<tr data-topic-id="51825" class="">
      <td class="topic-poster">

A to-do that got forgotten?

not really understand this. T_T

Just that if the class value contained a category name. CSS could be used to display: none them.

oh really… quite sad if this can’t be done

Yes, I find CSS fixes are often the easiest approach. For example, this found in “Suggested” below. If for some reason it was decided to not show topics in the “feature” category, it would be trivial to not display the row.

<div id="suggested-topics">
.....
<tr id="ember3635"
 class="ember-view topic-list-item category-feature"
 data-topic-id="38733">

To be fair, getting the code to put a category name in the class value may not be so easy.