We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46886
    • 1,154 Posts
    Hello all:

    I asked a friend of mine to create a flexible solution for recent posts and recent threads in Discuss. Rimo is a great programmer and handles any platform well. I asked him if it was ok to provide this solution to the community, and he was more than willing to let others use it.

    This dynamic tool has a lot of good options, it can provide new posts or threads, or use other data like replies or pageviews to identify the hottest topics. So its a powerful solution which can be applied in many ways. It allows you to customize length of the exerpt, number of posts and even use the author's username or "real" name.

    The one small drawback is that the output is hard-coded rather than using a chunk for styling, so to adjust the output order or style may need slight changes to the code (at the end, where he outputs the <li> and <p> and so on, another way is to target your styling to the surrounding <div>). I will probably mess with this part and can put a primer on that later if anyone needs it.

    Ok, here is the solution:


    <?php
            //Notes: if you set show_posts to 1 show_threads should be 0, only 1 filter is allowed - for posts only
            //we can show latest posts, but for show_threads we have several options, but only choose one
        //POST SETTINGS BELOW
        $show_posts = '1'; //Set to 1 if you want to show posts
        $posts_last_added = '1'; //Set to 1 if you want to show last added posts
        $posts_dynamic = '1'; //Set to 1 if you want to show dynamicly posts from current board
         
        //THREADS SETTINGS BELOW
        $show_threads = '0'; //Set to 1 if you want to show threads
        //SELECT ONLY 1 OF 3 OPTIONS BELOW
        $threads_last_posted = '0'; //Set to 1 if you want to show lastly active threads
        $threads_most_viewed = '0'; //Set to 1 if you want to show most viewed threads
        $threads_most_reply = '1'; //Set to 1 if you want to show threads with most reply's
     
        $order = 'DESC';  //Set ASC for A->Z or DESC for Z->A
        $limit = '10'; //Set limit count for results outputed which to be showed
        $letters_limit = '180'; //Set letters limit for description of posts messages
        $user_real_name = '1'; //Set to 1 if you want to show First Last names, set to 0 for username
         
        //DONT EDIT BELOW THIS LINE
        //COPYRIGHT RIMOTEVST KANEV
        //DONT EDIT BELOW THIS LINE
         
        //Build main sql querys
    if($show_threads == '1'){
        $c = $modx->newQuery('disThread');
    }else{
        $c = $modx->newQuery('disPost');
    }
        //Set filters
        if(($show_posts == '1') AND ($posts_last_added == '1')){
        $c->sortby('createdon',$order);
        }else{}
        if(($show_threads == '1') AND ($threads_last_posted == '1')){
        $c->sortby('post_last_on',$order);
        }else{}
        if(($show_threads == '1') AND ($threads_most_viewed == '1')){
        $c->sortby('views',$order);
        }else{}
        if(($show_threads == '1') AND ($threads_most_reply == '1')){
        $c->sortby('replies',$order);
        }else{}
     
    if($show_threads == '1'){  
        //Exclude private threads
        $c->where(array(
        'private' => '0',
        ));
    }else{
        //If we are in main board page
        if((strip_tags($_GET['board']) != '') AND ($posts_dynamic == '1')){
        $c->where(array(
        'board' => strip_tags($_GET['board']),
        ));
        }else{}
        //If we are in thread page
        if((strip_tags($_GET['thread']) != '') AND ($posts_dynamic == '1')){
        $t = $modx->newQuery('disThread');
        $t->where(array(
        'id' => strip_tags($_GET['thread']),
        ));
        $t->limit(1,0);
        $boards = $modx->getCollection('disThread',$t);
        foreach ($boards as $board) {
            $c->where(array(
            'board' => $board->get('board'),
            ));
        }
        }else{}
    }
        //Set limits
        $c->limit($limit,0);
     
        //Get the Collections fetchResults
    if($show_threads == '1'){
        $resources = $modx->getCollection('disThread',$c);
    }else{
        $resources = $modx->getCollection('disPost',$c);
    }
        //Start of output array
        $output = '';
         
        //Looping to fill the output
    foreach ($resources as $res) {
        if($show_threads == '1'){
            $output .= '<li class="rthread"><a href="forums/thread/'.$res->get('id').'/'.strtolower($res->get('title')).'">'.$res->get('title').'</a>';
            $a = $modx->getObject('disUser', array('id' => $res->get('author_first')));
            $output .= ' by <a class="rauthor" href="forums/u/'.$a->get('username').'/">';
        if($user_real_name == '1'){
            $output .= $a->get('name_first').' '.$a->get('name_last').'</a>';
        }else{
            $output .= $a->get('username').'</a>';
        }
            $output .= '</li>';
        }else{
            $output .= '<li class="rpost"><a href="forums/thread/'.$res->get('thread').'/'.strtolower($res->get('title')).'#dis-post-'.$res->get('id').'">'.$res->get('title').'</a>';
            $output .= '<p>'.mb_substr($res->get('message'), 0, $letters_limit).'</p>';
            $a = $modx->getObject('disUser', array('id' => $res->get('author')));
            $output .= ' by <a class="rauthor" href="forums/u/'.$a->get('username').'/">';
        if($user_real_name == '1'){
            $output .= $a->get('name_first').' '.$a->get('name_last').'</a>';
        }else{
            $output .= $a->get('username').'</a>';
        }
            $output .= '</li>';
        }
    }
        //Print the output
    return $output;
    


    Any feedback is more than welcome! Hope this tool will be helpful!
      • 46220
      • 66 Posts
      Yet still struggling with this, however, on my update profile page, it works! Have no clue why it does work on that specific page and not on any other page. Weird, but worth to investigate it further.
        • 46886
        • 1,154 Posts
        Ok let's troubleshoot this, because it worked for me without a problem. I want this to be useful to everyone, so I want to know what is going on here. Its so weird that it would work on one page but not others, but we have seen some signs that Discuss can be a bit quirky.

        If we work at it and get very clear about it, we can get some support from someone. But we have to be very clear on exactly what the problem is.

        First off, pls make sure your pages validate: validator.w3.org/ if there are errors, then all bets are off.

        Second, pls use precisely the code I provide below, which works on my site. If we can get it to work, then you can play with it to get exactly what you want.

        Call this snippet on your front page, main forum page, and on the update profile page. In addition, pls make a new resource (page), apply the relevant template, and put nothing in the contents except for the snippet call. That way we can get an idea about where it works and where it doesn't. If it only works on some pages it will help us troubleshoot.

        Ok, make this snippet called Discuss.Threadsviews:

        <?php
        //POST SETTINGS BELOW
        	$show_posts = '0'; //Set to 1 if you want to show posts
        	$posts_last_added = '0'; //Set to 1 if you want to show last added posts
        	$posts_dynamic = '0'; //Set to 1 if you want to show dynamicly posts from current board
        	
        	//THREADS SETTINGS BELOW
        	$show_threads = '1'; //Set to 1 if you want to show threads
        	//SELECT ONLY 1 OF 3 OPTIONS BELOW
        	$threads_last_posted = '0'; //Set to 1 if you want to show lastly active threads
        	$threads_most_viewed = '1'; //Set to 1 if you want to show most viewed threads
        	$threads_most_reply = '0'; //Set to 1 if you want to show threads with most reply's
        
        	$order = 'DESC';  //Set ASC for A->Z or DESC for Z->A
        	$limit = '5'; //Set limit count for results outputed which to be showed
        	$letters_limit = '30'; //Set letters limit for description of posts messages
        	$user_real_name = '1'; //Set to 1 if you want to show First Last names, set to 0 for username
        	
        	//DONT EDIT BELOW THIS LINE
        	//COPYRIGHT RIMOTEVST KANEV
        	//DONT EDIT BELOW THIS LINE
        	
        	//Build main sql querys
        if($show_threads == '1'){
        	$c = $modx->newQuery('disThread');
        }else{
        	$c = $modx->newQuery('disPost');
        }
        	//Set filters
        	if(($show_posts == '1') AND ($posts_last_added == '1')){
        	$c->sortby('createdon',$order);
        	}else{}
        	if(($show_threads == '1') AND ($threads_last_posted == '1')){
        	$c->sortby('post_last_on',$order);
        	}else{}
        	if(($show_threads == '1') AND ($threads_most_viewed == '1')){
        	$c->sortby('views',$order);
        	}else{}
        	if(($show_threads == '1') AND ($threads_most_reply == '1')){
        	$c->sortby('replies',$order);
        	}else{}
        
        if($show_threads == '1'){	
        	//Exclude private threads
        	$c->where(array(
        	'private' => '0',
        	));
        }else{
        	//If we are in main board page
        	if((strip_tags($_GET['board']) != '') AND ($posts_dynamic == '1')){
        	$c->where(array(
        	'board' => strip_tags($_GET['board']),
        	));
        	}else{}
        	//If we are in thread page
        	if((strip_tags($_GET['thread']) != '') AND ($posts_dynamic == '1')){
        	$t = $modx->newQuery('disThread');
        	$t->where(array(
        	'id' => strip_tags($_GET['thread']),
        	));
        	$t->limit(1,0);
        	$boards = $modx->getCollection('disThread',$t);
        	foreach ($boards as $board) {
        		$c->where(array(
        		'board' => $board->get('board'),
        		));
        	}
        	}else{}
        }
        	//Set limits
        	$c->limit($limit,0);
        
        	//Get the Collections fetchResults
        if($show_threads == '1'){
        	$resources = $modx->getCollection('disThread',$c);
        }else{
        	$resources = $modx->getCollection('disPost',$c);
        }
        	//Start of output array
        	$output = '';
        	
        	//Looping to fill the output
        foreach ($resources as $res) {
        	if($show_threads == '1'){
        		$output .= '<li class="rthread"><a href="forums/thread/'.$res->get('id').'/'.strtolower($res->get('title')).'">'.$res->get('title').'</a>';
        		$a = $modx->getObject('disUser', array('id' => $res->get('author_first')));
        		$output .= ' by <a class="rauthor" href="forums/u/'.$a->get('username').'/">';
        	if($user_real_name == '1'){
        		$output .= $a->get('name_first').' '.$a->get('name_last').'</a>';
        	}else{
        		$output .= $a->get('username').'</a>';
        	}
        		$output .= '</li>';
        	}else{
        		$output .= '<li class="rpost"><a href="forums/thread/'.$res->get('thread').'/'.strtolower($res->get('title')).'#dis-post-'.$res->get('id').'">'.$res->get('title').'</a>';
        		$output .= '<p>'.mb_substr($res->get('message'), 0, $letters_limit).'</p>';
        		$a = $modx->getObject('disUser', array('id' => $res->get('author')));
        		$output .= ' by <a class="rauthor" href="forums/u/'.$a->get('username').'/">';
        	if($user_real_name == '1'){
        		$output .= $a->get('name_first').' '.$a->get('name_last').'</a>';
        	}else{
        		$output .= $a->get('username').'</a>';
        	}
        		$output .= '</li>';
        	}
        }
        	//Print the output
        return $output;


        Then, use this snippet call:

        [[!Discuss.Threadsviews]]

        Finally, feed me back on what is going on, or better, put all this in there and let me go to the site and check it myself.

        Again, if there are underlying errors and the page won't validate, those have to be fixed first, there is just too much uncertainty when there are underlying errors.

        Ok, pls let me know, we can work this out with some effort.
          • 46220
          • 66 Posts
          Home page has one error about a meta name, but that should not be any problem in my opinion. I've also asked someone with more technical skills to look into it.

          I've created the snippet and a page with just the snippet call, but still nothing: http://www.gamerschain.com/test-discuss-threadviews.html
            • 46886
            • 1,154 Posts
            Hey I just got back, pls give me an update on this situation.

            Let's fix that one error and then move on, it doesn't sound like it would cause a problem but you never know.

            Then we may get to the bottom of this issue. If we are lucky smiley
              • 46220
              • 66 Posts
              Right..... want to hear something weird?

              I had this feeling it somehow was not making a connection to discuss and the only "outside-of-discuss" page that did produce results, I just thought, why not include some of the code in there. So I did and I ended up pasting
              [[!DiscussUpdateProfileLoader]]
              into the chunk that should display popular threads. The result: it showed latest posts! Though very weird, it is much more than I expected, so now I am going to test some other things.

              It is a good thing I like to experiment, and also a good thing it is not with nuclear devices laugh

              EDIT: I included into my sidebar chunk and it is working now laugh. Not the ideal solution, but it is working now!

              EDIT 2: And also noticed that latest posts shows private messages.... I thought that was covered in the snippet? smiley [ed. note: gormytorysh last edited this post 9 years, 7 months ago.]
                • 46886
                • 1,154 Posts
                Hahaha!! really!!

                Hmm...that's so great. I thought the profile loader was already in there...

                Um, for private, it should be here:

                if($show_threads == '1'){  
                    //Exclude private threads
                    $c->where(array(
                    'priv


                But that's just for threads, not posts. I have all three types of thread contents on my front page, but no posts contents at all...

                Ah, right now I am only using the dynamic posts within some categories, and it only looks at the posts in that category. So i guess I haven't seen this problem before.

                I do think we could put in a line of php into the last part of the above code, to strip out private messages, or maybe it needs to be done earlier.
                  • 46886
                  • 1,154 Posts
                  How about put in a new clause or whatever its called, like this:
                  if($show_threads == '1'){  
                      //Exclude private threads
                      $c->where(array(
                      'private' => '0',
                      ));
                  if($show_posts == '1'){  
                      //Exclude private threads
                      $c->where(array(
                      'private' => '0',
                      ));


                  As I understand, to show the dynamic or last posts, you need to set the show_posts to '1', so this clause *should* jump in and strip out the private things. But it won't, now that I think about it, because there is no private flag for posts according to here: http://rtfm.modx.com/extras/revo/discuss/discuss.database-model

                  Looking at the values available, I wonder if we could use the post flags regarding the board and thread it is in. For PMs, the board may appear as a specific number or it may be null...

                  I just checked, here is my url of a private message: forums/messages/view/?thread=248#dis-post-392

                  But I notice that the urls of normal posts also have no boards listed. That's just the url though, the value is another matter...
                    • 46886
                    • 1,154 Posts
                    Hmm this has got me thinking...we could pull out the threads with private flag before getting the posts, but the query is straight to the posts, not the threads.

                    This is a tricky coding question. I am guesing this is perhaps the best place to put in the private limiter:

                    }else{
                        $c = $modx->newQuery('disPost');
                    }


                    Because this is where we are getting the posts, and it seems its the right place to strip out the private posts.
                      • 46220
                      • 66 Posts
                      Sorted.

                      I assumed that I could only use post settings OR thread settings. But I am not using show_posts, so I can actually set show_threads to 1.
                      Yay, another issue solved and one more major issue and I can go into beta laugh.
                      Thanks for helping mate smiley


                      Spoke too soon. That only shows threads and not posts.... somehow I need to exclude messages from the query. As I see it, http://www.gamerschain.com/forums/messages/view?thread=15#dis-post-26 is a separate part of the forum and not a board or thread. I found something how to exclude data in php which I'll try, but surely needs adjustment (if working at all that is).

                      $excludeArray = array('value1','value2') ;
                      [ed. note: gormytorysh last edited this post 9 years, 7 months ago.]