The discussion revolves around creating a SQL query in Discourse’s Data Explorer to pull the latest topics, which will be displayed on a Wordpress homepage using the Twig Anything plugin.
sunnyt7 is seeking help with writing the SQL query, as they have no experience with it. pfaffman shares a link to a thread with SQL examples, but it’s mostly about badges. sunnyt7 then shares their progress with the SQL query, which pulls the necessary fields, including user avatars.
The query is as follows:
SELECT
t.last_post_user_id, ua.custom_upload_id, u.username, u.username_lower, t.title, t.slug, t.id, t.category_id, c.name, c.color, t.posts_count, t.last_posted_at
FROM
topics t
LEFT JOIN categories c ON c.id = t.category_id
LEFT JOIN users u ON u.id = t.last_post_user_id
LEFT JOIN user_avatars ua ON ua.user_id = t.last_post_user_id
WHERE
t.archetype = 'regular'
AND
t.last_posted_at IS NOT NULL
AND
t.visible IS TRUE
AND
t.deleted_at IS NULL
ORDER BY
t.last_posted_at DESC
LIMIT
10
sunnyt7 then encounters issues with formatting the timestamp and displaying replies instead of posts. pfaffman suggests using a Rails function to format the timestamp, while meglio recommends using a Twig filter or formatting the date directly in the SQL query.
sunnyt7 eventually figures out how to format the timestamp using Twig syntax. The final result is a displayed list of latest topics on their Wordpress homepage, with formatted timestamps.