Get First Topics Post In The Last Topics Widget
Quote from alireza on May 22, 2020, 11:07 pmHello, Thanks for your great plugin.
Im trying to show latest topics from a forum and their summary in my home page.
I used i widget shortcode and changed some styles from the widgets php and its working well.
My problem is that excerpt shows the latest post but i wanna see the first post in topic, i think i should change something in below query but i dont know how to do it. what should i do to change this? thanks.
$elements = $this->asgarosforum->db->get_results("SELECT p.id, p.text, p.date, p.parent_id, p.author_id, t.name, (SELECT COUNT(*) FROM {$this->asgarosforum->tables->posts} WHERE parent_id = p.parent_id) AS post_counter FROM {$this->asgarosforum->tables->posts} AS p LEFT JOIN {$this->asgarosforum->tables->topics} AS t ON (t.id = p.parent_id) WHERE p.forum_id IN({$available_forums}) AND p.id IN (SELECT MAX(p_inner.id) FROM {$this->asgarosforum->tables->posts} AS p_inner GROUP BY p_inner.parent_id) AND t.approved = 1 ORDER BY t.id DESC LIMIT {$number};");
Hello, Thanks for your great plugin.
Im trying to show latest topics from a forum and their summary in my home page.
I used i widget shortcode and changed some styles from the widgets php and its working well.
My problem is that excerpt shows the latest post but i wanna see the first post in topic, i think i should change something in below query but i dont know how to do it. what should i do to change this? thanks.
$elements = $this->asgarosforum->db->get_results("SELECT p.id, p.text, p.date, p.parent_id, p.author_id, t.name, (SELECT COUNT(*) FROM {$this->asgarosforum->tables->posts} WHERE parent_id = p.parent_id) AS post_counter FROM {$this->asgarosforum->tables->posts} AS p LEFT JOIN {$this->asgarosforum->tables->topics} AS t ON (t.id = p.parent_id) WHERE p.forum_id IN({$available_forums}) AND p.id IN (SELECT MAX(p_inner.id) FROM {$this->asgarosforum->tables->posts} AS p_inner GROUP BY p_inner.parent_id) AND t.approved = 1 ORDER BY t.id DESC LIMIT {$number};");Uploaded files:
Quote from qualmy91 on May 23, 2020, 7:24 amHey @alireza,
yes this query get’s all the posts for the widget. Please be aware that all changes on the plugin files will be deleted with every update of Asgaros Forum. So you have to do all this changes again after every update. This also applies to all changes to the CSS files. To get this changes permanently you should write a own plugin to create a custom widget.
But back to your question. To get the first post of the last topic you have to change
MAX(p_inner.id)
toMIN(p_inner.id)
:$elements = $this->asgarosforum->db->get_results("SELECT p.id, p.text, p.date, p.parent_id, p.author_id, t.name, (SELECT COUNT(*) FROM {$this->asgarosforum->tables->posts} WHERE parent_id = p.parent_id) AS post_counter FROM {$this->asgarosforum->tables->posts} AS p LEFT JOIN {$this->asgarosforum->tables->topics} AS t ON (t.id = p.parent_id) WHERE p.forum_id IN({$available_forums}) AND p.id IN (SELECT MIN(p_inner.id) FROM {$this->asgarosforum->tables->posts} AS p_inner GROUP BY p_inner.parent_id) AND t.approved = 1 ORDER BY t.id DESC LIMIT {$number};");
Hey @alireza,
yes this query get’s all the posts for the widget. Please be aware that all changes on the plugin files will be deleted with every update of Asgaros Forum. So you have to do all this changes again after every update. This also applies to all changes to the CSS files. To get this changes permanently you should write a own plugin to create a custom widget.
But back to your question. To get the first post of the last topic you have to change MAX(p_inner.id)
to MIN(p_inner.id)
:
$elements = $this->asgarosforum->db->get_results("SELECT p.id, p.text, p.date, p.parent_id, p.author_id, t.name, (SELECT COUNT(*) FROM {$this->asgarosforum->tables->posts} WHERE parent_id = p.parent_id) AS post_counter FROM {$this->asgarosforum->tables->posts} AS p LEFT JOIN {$this->asgarosforum->tables->topics} AS t ON (t.id = p.parent_id) WHERE p.forum_id IN({$available_forums}) AND p.id IN (SELECT MIN(p_inner.id) FROM {$this->asgarosforum->tables->posts} AS p_inner GROUP BY p_inner.parent_id) AND t.approved = 1 ORDER BY t.id DESC LIMIT {$number};");