View Full Version : Default post order in thread display for unauthenticated users
El Queso
10-28-2010, 05:30 AM
I've searched for quite awhile for something on this and can't find what I'm looking for. Admittedly I'm new at vBulletin programming - the answer may be relatively simple, but for a newby there is an overwhelming amount of information to sift through!
Is there a way to change the default order in which posts are displayed when displaying a thread to users who are not logged in?
The default order is earliest posts first. I have a large forum with threads that reach hundreds (some actually thousands) and go 'way back in time. When unauthenticated users are browsing the forum, I want them to see the latest posts in a thread first so they see the latest information.
I can't find any admin options that allow me to set this default for any but logged in users. I'm assuming I will have to either use a plugin/hook or do a hack, but I can't figure out what template, code file, hook and/or variable I need to look at to make this happen.
Meestor_X
02-20-2011, 10:15 PM
I too want to do this.
I want visitors to first see the highest rated threads, but registered users to see the default sort order (in my case most recent first)...
Or even better to have it change via a promotion. Sort order changes after you make one post or something like that.
El Queso
02-21-2011, 02:13 AM
I probably should have returned and posted my solution for other's reference.
The problem I found with trying to set a variable was I didn't (and still don't) completely understand how the user data is being generated when the user is a guest.
The code that determines the sort order is in SHOWTHREAD.PHP. The value it uses is the guest user's "postorder" variable ($vbulletin->userinfo['postorder']). The standard guest user name in the version that comes out of the box is "Unregistered". I don't have a user with the user name "Unregistered" so obviously vBulletin is doing something on authentication to create that user and its information. I don't know if there's something else in the admin portions of vBulletin that allows one to set the "Unregistered" user's default info or not, but I couldn't find it if there is.
Doing this from a hook seems to be problematic. You have to be able to set the $vbulletin->userinfo['postorder'] to '' (empty string) to make it work (see the code below for how it generates the sort order for the query), but when I tried to do this in a plug-in, using the 'showthread_start' hook, I got no changes in my sort order. Maybe this property unassignable, or the user (or its information) gets recreated every postback because it's not a database user? I don't know.
What I did to solve the problem was to hack the SHOWTHREAD.PHP file thusly:
Find this code ("set post order" should find it directly):
// set post order
if ($vbulletin->userinfo['postorder'] == 0)
{
$postorder = '';
}
else
{
$postorder = 'DESC';
}
and change it to this:
// set post order
if ($vbulletin->userinfo['postorder'] == 0 AND $vbulletin->userinfo['username'] != 'Unregistered')
{
$postorder = '';
}
else
{
$postorder = 'DESC';
}
NOTE that I read something about the user name for language packs other than the default English may well have other user names than "unregistered" and therefore may not work in those situations.
As far as going further with highest rated threads sorting and/or promoting threads - you can see where to make the changes to the sort order itself, but it will be a bit more complex than I laid out than for just a user that's not logged in. You will obviously have to get the other info you are interested in and make the conditional fit your needs.
Meestor_X
02-21-2011, 04:37 AM
Thanks for that information!
I'm trying to understand that code (I'm only a beginner in PHP)...
This code seems to deal only with ascending and descending order for the posts, by date. (I assume that's what dateline means?)
What if they're sorted by thread rating or some other factor? Where is that taken into account?
The query seems to be:
$getpostids = $db->query_read("
SELECT postid
FROM " . TABLE_PREFIX . "post AS post
$hook_query_joins
WHERE threadid = $threadid
AND visible = 1
$hook_query_where
ORDER BY dateline $postorder
LIMIT $limitlower, $perpage
");-Andy.
El Queso
02-21-2011, 05:24 PM
You hit on a good question - "What if they're sorted by thread rating or some other factor? Where is that taken into account?"
I've been making a lot of admin moderation additions for a friend of mine who bemoans the complete lack of moderation "extras" in vBulletin. The problem there is that there are no templates to be used and I have found myself making changes to the code itself in almost all instances, when related specifically to admincp stuff.
With the user experience (in this case, specifically SHOWTHREAD.PHP) you have a bit more options because you can modify templates and put code in plugins. But you will still have situations (like this, it appears) that will cause you a problem. If vBulletin has not left you a hook and also variables to modify via the hook, then you really have no choice but to modify the PHP code itself.
In this case, as far as I'm aware vBulletin does not have any ability to set what the thread sort order field itself is - only which direction by date you can change. In the user profile you can only change the thread order, you can't set what field you want to sort on, or what calculation, etc.
In fact, as far as changing the sort order goes, you may actually be able to hook in and change variables befre the query is run. I saw this when I went back in to look and answer some of your questions. Look for "vBulletinHook::fetch_hook" to find what hook plugin events the PHP code is processing. There are various hooks for various operations within SHOWTHREAD.PHP. I know this doesn't help you much, I just metnion it in case someone else is reading to augment the information I previously posted.
But the sort order of "dateline" (yeah, it's the UNIX date/time of the post) seems to be the only field on which vBulletin, out of the box, allows us to change the sort order of the thread display query.
The only way I see for you to do this would be to change the query itself by adding a variable of your own, with your own logic to determine the sort order. It's not easy, not for the squeamish (or uncertain).
Remember that when you upgrade, there is a chance that vBulletin will have made modifications to whatever files you have changed, and therefore overwrite any changes you've made when you upgrade to a later version. If you DO make any changes, make sure you comment them in your code so you can go back and make the changes again in new versions.
BTW - I don't know if you've seen these links:
https://vborg.vbsupport.ru/showthread.php?t=256185&highlight=%24vbulletin
https://vborg.vbsupport.ru/showthread.php?t=99570&highlight=create+product+template
I have a hard time finding good information on really technical issues, but those two threads are valuable and not all that easy to find either. They will help you with understanding how some of the "guts" of vBulletin works.
I'm by trade a .Net developer. I found it (and still do in many ways) extremely difficult to get into the whole mindset of procedural programming and a lack of good object-oriented programming. However, the more I learn the easier it becomes to do stuff. I need to go back and remember the days of ASP programming and then it's just a matter of syntax and particulars of vBulletin's API and templates.
I don't know what your background is, but if it's a developer in another language than PHP, the hardest part of this is not only learning PHP, but tying in what vBulletin's API (through the objects it does use, like the data managers, the $vbulletin object, etc) does, their use of templates to easily modify user interface, and so on.
If you're not a developer to begin with, however, I have to say that I would not recommend modifying the PHP files to any great extent, and of course always make sure you have a backup of the original so you can things back to the way they were.
Adrijan
02-14-2013, 03:34 AM
Is there a way to change the default order in which posts are displayed when displaying a thread to users who are not logged in?
The default order is earliest posts first. I have a large forum with threads that reach hundreds (some actually thousands) and go 'way back in time. When unauthenticated users are browsing the forum, I want them to see the latest posts in a thread first so they see the latest information.
I can't find any admin options that allow me to set this default for any but logged in users. I'm assuming I will have to either use a plugin/hook or do a hack, but I can't figure out what template, code file, hook and/or variable I need to look at to make this happen.
You need add this PHP code to hook location ... showthread_start ...
Admin CP > Plugin Manager > Add new plugin >
Product : vBulletin
Hook location : showthread_start
Title : Change Display Posts Order For Guests - Newest First
Execution Order : 5
Plugin is Active : Yes
Plugin PHP Code :
if ($vbulletin->userinfo['userid'] == 0) { $vbulletin->userinfo['postorder'] = 1; }
:)
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.