Show random post?

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!

4 Likes

cool idea. this could help surface up older content.

4 Likes

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.

2 Likes

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”?

https://meta.discourse.org/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.

2 Likes

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:

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

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 )
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 
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.