Notifications
Clear all

[Solved] Showing number of new / unread posts per thread.

5 Posts
2 Users
1 Likes
1,298 Views
sc89me
Posts: 26
Topic starter
(@sc89me)
Eminent Member
Joined: 4 years ago

As the title says I am currently looking for a way to add or get rather the number of new posts in a forum or thread to display this as a number in a modified simple layout. 

So as an example you would get "Topic title" then like "8 new posts" this would be specific to current user. 

Is there a function to get this or can anyone make any recommendations on how to achieve this? 

Can work in PHP but I am only support for the website using this plugin so know very little in regards to how it works and I don't see a PHP function breakdown in the docs to point me in the right direction of what function calls I would need to make to modify the template to support this change. 

4 Replies
sc89me
Posts: 26
Topic starter
(@sc89me)
Eminent Member
Joined: 4 years ago

Bumpidy bump.

Alvina
Posts: 1869
Moderator
(@alvina)
Member
Joined: 5 years ago

Hi @sc89me,

Sorry for the late response.

Please follow the steps below to get it resolved:

1. Open the /wp-content/plugins/wpforo/wpf-includes/class-logs.php file.

2. Find the following class wpForoLogs{}.

3. Put the JS code below at the end of wpForoLogs{} class. (line 366 - if you use the current wpForo 1.6.5 version) :

/**
* get unread posts count for topic by giving topicid
*
* @param int $topicid
*
* @return int
*/
public function get_topic_unreads_count($topicid){
$topicid = intval($topicid);
$read_topics = $this->get_read_topics();
$last_read_postid = (int) wpfval($read_topics, $topicid);
$sql = "SELECT SQL_NO_CACHE COUNT(*) FROM `".WPF()->tables->posts."`
WHERE `status` = 0
AND `topicid` = {$topicid}
AND `postid` > {$last_read_postid}";
$count = (int) WPF()->db->get_var($sql);
return (int) apply_filters('wpforo_logs_get_topic_unreads_count', $count, $topicid, $last_read_postid);
}

4. For using this function, you just need to call it, for example in this way:

$topicid = 955;
echo WPF()->log->get_topic_unreads_count($topicid);

Don't forget to delete all caches and press CTRL + F5(twice)  on the frontend before checking.

sc89me
Posts: 26
Topic starter
(@sc89me)
Eminent Member
Joined: 4 years ago

Thanks man I will take a look at this but it looks good thanks should let me move forward. 

sc89me
Posts: 26
Topic starter
(@sc89me)
Eminent Member
Joined: 4 years ago

Thanks again @Alvina

I didn't modify my plugin to use the code you have, I added it as a function within my theme to avoid this being over-written in an update. 


public static function get_topic_unreads_count($topicid)
{
$topicid = intval($topicid);
$read_topics = WPF()->log->get_read_topics();
$last_read_postid = (int)wpfval($read_topics, $topicid);
$sql = sprintf('SELECT SQL_NO_CACHE COUNT(*) FROM `%s` WHERE `status` = 0 AND `topicid` = %s AND `postid` > %s',
WPF()->tables->posts,
$topicid,
$last_read_postid
);
$count = (int)WPF()->db->get_var($sql);
return (int)apply_filters('wpforo_logs_get_topic_unreads_count', $count, $topicid, $last_read_postid);
}

I used that plus: 

intval(App::get_topic_unreads_count($topic['topicid']))

In the output. (App is the class in my theme I put this) it seems to work out nice I just had to change your "get_read_topics" refernece to point it back to the log class. 

Is a simmilar query available for parent forums to show the amount of unread posts across all topics within a forum? 

I am sorry for asking now instead of in my first post.