HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media


Welcome to TinyPortal. Please login or sign up.

July 16, 2024, 07:50:36 PM

Login with username, password and session length
  • Total Posts: 195,319
  • Total Topics: 21,233
  • Online today: 79
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 64
  • Total: 64

Top posts (most liked) module

Started by IgorS, April 08, 2024, 07:39:03 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.



Is there any way to add the module to SMF 2.1.4 with the most liked posts in X days?
Something like "Recent Posts" from TP but sorted with "likes".

I have used the modification Good Post/Bad Post in 2.0.19 and there was a module added below the forum, with the most liked posts in the last 7 days.


It does not yet exist, but should be possible. Thinking about it: we normally use SMF SSI functions, but I don't think there is an existing function offered that uses likes atm...

So someone would have to create the query...
Running Latest TP on SMF2.1 at:


Okay, challenge accepted...

Drop below code into a PHP block and you should be good to go..
There is one parameter you can change to influence the history: $days = '365' change 365 to the period in days you want...

global $scripturl, $smcFunc, $txt, $days, $modSettings;

// Configuration
// Specify number of days for likes.
    $days = '365';
// End Config

// get the likes

    $max_liked = 1;
    $request = $smcFunc['db_query']('', '
        SELECT m.id_msg, m.subject, l.likes, m.total_likes, m.id_board, m.id_topic, t.approved
        FROM (
            SELECT n.id_msg, n.subject, n.likes AS total_likes, n.id_board, n.id_topic
            FROM {db_prefix}messages as n
            ) AS m
            INNER JOIN
                (SELECT content_id, content_type, count(*) AS likes FROM {db_prefix}user_likes where content_type = "msg" AND like_time > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL {int:days} DAY)) group by content_id, content_type order by likes desc) AS l ON (l.content_id = m.id_msg)
            INNER JOIN {db_prefix}topics AS t ON (m.id_topic = t.id_topic)
            INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
                AND b.id_board != {int:recycle_board}' : '') . ')
        WHERE {query_see_board}' . ($modSettings['postmod_active'] ? '
            AND t.approved = {int:is_approved}' : '') . '
        ORDER BY l.likes DESC
        LIMIT 10',
            'recycle_board' => $modSettings['recycle_board'],
            'is_approved' => 1,
            'days' => $days,
    $messages = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $messages[] = array(
            'id' => $row['id_topic'],
            'subject' => $row['subject'],
            'num' => $row['likes'],
            'href' => $scripturl . '?msg=' . $row['id_msg'],
            'link' => '<a href="' . $scripturl . '?msg=' . $row['id_msg'] . '">' . $row['subject'] . '</a>'

        if ($max_liked < $row['likes'])
            $max_liked = $row['likes'];

    foreach ($messages as $i => $request)
        $messages[$i]['percent'] = round(($request['num'] * 100) / $max_liked);
// output the likes
    echo '
        <div class="content">
            <div class="title_bar">
                <h4 class="titlebg">
                    <span class="main_icons liked_messages"></span> ',  $txt['top_liked_messages'], ' -
                    ',$days,' ',$txt['days_word'],'
            <dl class="stats">';

    foreach ($messages as $item)
        echo '
                    ', $item['link'], '
                <dd class="statsbar generic_bar righttext">';

        if (!empty($item['percent']))
            echo '
                    <div class="bar" style="width: ', $item['percent'], '%;"></div>';
            echo '
                    <div class="bar empty"></div>';

        echo '
                    <span>', $item['num'], '</span>

    echo '
        </div><!-- .content -->';

You cannot view this attachment.
Running Latest TP on SMF2.1 at:


Just created the blockcode for top like users as well
Will be included in the next version..

You can move these two files in to the folder \tp-files\tp-blockcodes in your forum...
You can select them as blockcodes into PHP blocks...

You cannot view this attachment.
Running Latest TP on SMF2.1 at:


WOW! Thanks a lot!

Will try it soon and get back to you with some feedback!


Created two more, and updated the codes to allow removal of the title line in the block...

Running Latest TP on SMF2.1 at:


Verified and works great.

Would it be possible to unify look of those two: Most liked posts (left) and Recent posts (right)?
I would like to add "by User(link_to_account)" and Time below if possible.

You cannot view this attachment.

Where could we find something called "short_subject"? I see that Recent Posts block use such variable, which use regular subject value with "Re: " removed from messages.


Ok, I was able to add "sent by: User(link)":
You cannot view this attachment.

Modified blockcode attached.

Is this possible to change width of percent bars to 25% (now they are 50% of the block), or set entire panel to have 75% for "Most liked posts" and 25% for "Recent posts" blocks?

I have added "style="width:25%;float:right" to line: "<dd class="statsbar generic_bar righttext">';" but it looks like text with links is still using only 50% of block width:
You cannot view this attachment.

Finally, was able to style it right. V2 attached.


QuoteIs this possible to set entire panel to have 75% for "Most liked posts" and 25% for "Recent posts" blocks?

This is an option I am considering to include in the next TP version.

Still work in progress though

I understand you are all set now?
Running Latest TP on SMF2.1 at:


Yes, thank you so much!

BTW, if you're considering some updates/bugfixes, probably padding-top should be added to lower-panel, as showed below (red line):
You cannot view this attachment.


Actually, I already fixed that in the new package...

But thanks for reporting it

You cannot view this attachment.
Running Latest TP on SMF2.1 at:


I incorporated a setting in the blockcode to controle display of username: the width setting is something admins can do themselves...
Running Latest TP on SMF2.1 at: