Show random post?


(Vincent) #1

Hi,

I’m looking for a way to add a link (maybe in the header) to show a random post, preferably from a list of chosen categories.
How would I achieve that?

Thanks!


#2

cool idea. this could help surface up older content.


(Tobias Eigen) #3

An I'm feeling lucky button on search?


(Vincent) #4

More like a “random article” link on Wikipedia.


(Kane York) #5

This is what Suggested Topics at the bottom does, if you don’t have any new/unread.


(Jeff Atwood) #6

Correct! With the caveat that it tries to stay in the same category you are currently in, also we disallow any suggestions older than 1 year. That can be adjusted via site setting.


(Vincent) #7

Can I build a “suggested topics” page then?


(Mittineague) #8

How do you envision it being different from “top”?

https://meta.discourse.org/top

(Vincent) #9

I don’t know. Page would display random topics, not top topics?
Actually that’s not even what I asked first. What I would like to have is:

“Random post” (link) – click --> Discourse displays a random post from selected categories of the forum

But since this is a discussion I’m glad to hear what the people have to say :slight_smile:


(Mittineague) #10

I think a link to a random topic would be doable. It would need to filter to public topics to avoid giving an “oops” page. I don’t know if it would be more efficient to generate te number, check to make sure it’s OK and if not try again. Or get all the OK numbers and then randomize using that. I’m thinking the first would be better, but that’s only a guess.


(Vincent) #11

I’ll run tests with the data explorer. Coming up with the right request(s) might be a good start I think :face_with_monocle:


(Mittineague) #12

I think getting the topic count(distinct ids) from badge_posts would do for an idea of the ratio of OK to not OK when compared to the max(id) of the topics table.


(Christoph) #13

Just an idea: instead of making it entirely random, you could make it a “show me something unusual” page or “take me outside my bubble” by limiting the random posts to posts in topics that the user has never entered.


(Vincent) #14

First steps with the data explorer plugin here. I got this query up and running:

select * from topics
where category_id between 5 and 8
and random() < 0.005 limit 1

Supposing I kept it as is, how can I exploit its results on a public page?


(Régis Hanol) #15

A better query :wink:

SELECT * 
FROM topics
WHERE category_id BETWEEN 5 AND 8
ORDER BY random()
LIMIT 1

(Vincent) #16

I read a few articles regarding performances of order by random() queries. Your request actually takes twice as much time to return a result – but I reckon that the size of my community makes it insignificant.


(Mittineague) #17

I think it would be less expensive to pick off the top card from a shuffled category deck than it would be from topics,

I don’t care much for the “BETWEEN”. It would be like me to forget to change the numbers if the categories changed. Maybe you want some of the things I’ve NOTted but I think this would be less fragile and it seems to work well

WITH public AS (SELECT id AS public_cat_ids
                FROM categories
                WHERE NOT read_restricted
                AND name NOT LIKE 'Uncategorized'
                ORDER BY random()
                LIMIT 5 )
SELECT * 
FROM topics, public
WHERE category_id IN (public.public_cat_ids)
AND visible
AND NOT closed
AND NOT archived
ORDER BY random()
LIMIT 5