Don't run queries inside loops.
Use a single query using JOIN(s) to get all the data you need.
Example...
SELECT t.id as ThreadID
, title
, LEFT(thread_body, 50) as body
, date_format(threadtimestamp, '%D %b %Y %l:%i %p') as ftime
, count(p.id) as num_posts
FROM thread t
LEFT JOIN post p ON t.id = p.thread_id
GROUP BY t.id
ORDER BY threadtimestamp DESC;
+----------+----------+----------------------------------------------------+-----------------------+-----------+
| ThreadID | title | body | ftime | num_posts |
+----------+----------+----------------------------------------------------+-----------------------+-----------+
| 2 | Thread 2 | Pellentesque porttitor, velit lacinia egestas auct | 6th Apr 2021 1:52 PM | 3 |
| 1 | Thread 1 | Lorem ipsum dolor sit amet, consectetuer adipiscin | 5th Apr 2021 10:25 AM | 4 |
+----------+----------+----------------------------------------------------+-----------------------+-----------+