Show random post?


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?



cool idea. this could help surface up older content.


An I'm feeling lucky button on search?

More like a “random article” link on Wikipedia.

1 Like

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


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.

1 Like

Can I build a “suggested topics” page then?

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

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:

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.

1 Like

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:

1 Like

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.

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.


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?

1 Like

A better query :wink:

FROM topics
WHERE category_id BETWEEN 5 AND 8
ORDER BY random()

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.

1 Like

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 )
FROM topics, public
WHERE category_id IN (public.public_cat_ids)
AND visible
AND NOT closed
AND NOT archived
ORDER BY random()