Since version 2 of bbPress was released some users haven’t been happy with their options for a “Recent Replies” widget. Shortcode below as written to provide flexibility.
Shortcode: [bbpress_recent_replies_by_topic]
.
It allows for a parameter to determine the number of replies shown (defaults to 5). That parameter is “show”, and is used in the shortcode like so: [bbpress_recent_replies_by_topic show=10]
The shortcode gathers the 5 most recent replied-to topics, and their most recent replies. Then, while looping through the replies, it calls the 2nd function to output a row of data. You should only edit the “custom_bbpress_recent_reply_row_template()” function to change the HTML.
<?php
/*
* Get the most recently replied-to topics, and their most recent reply
*/
function custom_bbpress_recent_replies_by_topic($atts){
$short_array = shortcode_atts(array('show' => 5, 'forum' => false, 'include_empty_topics' => false), $atts);
extract($short_array);
// default values
$post_types = array('reply');
$meta_key = '_bbp_last_reply_id';
// allow for topics with no replies
if ($include_empty_topics) {
$meta_key = '_bbp_last_active_id';
$post_types[] = 'topic';
}
// get the 5 topics with the most recent replie
$args = array(
'posts_per_page' => $show,
'post_type' => array('topic'),
'post_status' => array('publish'),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => $meta_key,
);
// allow for specific forum limit
if ($forum){
$args['post_parent'] = $forum;
}
$query = new WP_Query($args);
$reply_ids = array();
// get the reply post->IDs for these most-recently-replied-to-topics
while($query->have_posts()){
$query->the_post();
if ($reply_post_id = get_post_meta(get_the_ID(), $meta_key, true)){
$reply_ids[] = $reply_post_id;
}
}
wp_reset_query();
// get the actual replies themselves
$args = array(
'posts_per_page' => $show,
'post_type' => $post_types,
'post__in' => $reply_ids,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
ob_start();
// loop through results and output our rows
while($query->have_posts()){
$query->the_post();
// custom function for a single reply row
custom_bbpress_recent_reply_row_template( $query->current_post + 1 );
}
wp_reset_query();
$output = ob_get_clean();
return $output;
}
add_shortcode('bbpress_recent_replies_by_topic', 'custom_bbpress_recent_replies_by_topic');
/*
* Executed during our custom loop
* - this should be the only thing you need to edit
*/
function custom_bbpress_recent_reply_row_template( $row_number ){
// get the reply title
$title = get_the_title();
// optional title adjustments -- delete or comment out to remove
// remove "Reply To: " from beginning of title
$title = str_replace('Reply To: ', '', $title);
// trim title to specific number of characters (55 characters)
$title = substr( $title, 0, 55);
// trim title to specific number of words (5 words)...
$title = wp_trim_words( $title, 5, '...');
// determine if odd of even row
$row_class = ($row_number % 2) ? 'odd' : 'even';
?>
<div class="bbpress-recent-reply-row <?php print $row_class; ?>">
<div><?php print $title; ?></div>
<div>Excerpt: <?php the_excerpt(); ?></div>
<div>Author: <?php the_author(); ?></div>
<div>Link To Reply: <a href="<?php the_permalink(); ?>">view reply</a></div>
<div>Link To Topic#Reply: <a href="<?php print get_permalink( get_post_meta( get_the_ID(), '_bbp_topic_id', true) ); ?>#post-<?php the_ID(); ?>">view reply</a></div>
<div>Link To Topic/page/#/#Reply: <a href="<?php bbp_reply_url( get_the_ID() ); ?>">view reply paged</a></div>
<div>Date: <?php the_date(); ?></div>
<div>Avatar: <?php print get_avatar( get_the_author_meta( 'ID' ) ); ?></div>
<div>Time Ago: <?php print human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div>
<div>bbPress Profile Link: <?php print bbp_user_profile_link( get_the_author_meta( 'ID' ) ); ?></div>
<div>Avatar linked to bbPress Profile:<a href="<?php print esc_url( bbp_get_user_profile_url( get_the_author_meta( 'ID' ) ) ); ?>"><?php print get_avatar( get_the_author_meta( 'ID' ) ); ?></a></div>
</div>
<?php
// Refs
// http://codex.wordpress.org/Template_Tags#Post_tags
// http://codex.wordpress.org/Function_Reference/get_avatar
// http://codex.wordpress.org/Function_Reference/human_time_diff
// (template tags for bbpress)
// https://bbpress.trac.wordpress.org/browser/trunk/src/includes/users/template.php
// https://bbpress.trac.wordpress.org/browser/trunk/src/includes/replies/template.php
}
// allow shortcodes to run in widgets
add_filter( 'widget_text', 'do_shortcode');
// don't auto-wrap shortcode that appears on a line of it's own
add_filter( 'widget_text', 'shortcode_unautop');
Discussion
Awesome! Latest topic with reply and without reply !! [bbpress_recent_replies_by_topic include_empty_topics=1]. For me better is shortcode than plugin without shortcode. That is more usefull!
Thanks and goodluck Daggerhart
Is there any way of adding custom post types?
All post types on my website has it’s own topic for replies instead of wp comments with bbPress Topics for Posts.
The only downside is when one follows a recent reply link, they’ll get to the forum topic created by the bbPress Topics for Posts plugin, I’d rather have them end up on the page of a custom post type.
A recent posts code was easy made but doesn’t serve the need.
hey,
In what file can i write your code? I looking in bbpress.php etc via search and dont see a “custom_bbpress..” Thnak you for a reply.
Hi Glados,
You can put this code in your theme’s functions.php file, or in a custom plugin if you have one. You should edit any of the files that come with bbpress.
When i copy this into function my website stop working.
I try: https://gist.github.com/daggerhart/09b0dbc7eb766486353b#file-bbpress_recent_replies-shortcode-php i donwload in and copy a php file into child theme + /plugins/bbpress and trying to use a shortcode in text widget like that: [bbpress_recent_replies_by_topic] still no working. Any idea? ;) Thank you.
First approach– If you copy it to your functions.php file, you need to remove the first line of this code:
<?php
Second approach– If you add this as a new file in your theme folder, you have to “include” the new file by adding some code like this to your functions.php:
include_once get_stylesheet_directory()."/bbpress_recent_replies-shortcode.php";
If you have trouble with either of those approaches, this might not be the right solution for you. Using this relies on some understanding of PHP.
Hope this helps, sorry if it doesn’t work out,
Jonathan
Still working thx, need some changes in my theme. Thanks for a reply ;)
Brilliant and easy to understand code! You are awesome thank you sooooooo much!
Hi This shortcode is working amazingly, what I want is need pagination at the end of page on first page only 10 replies will show and on next page 10 replies and so on and user will also search replies please reply
Hi,
Great shortcode!
Can you explain how “// allow for specific forum limit” works? I cannot get it to work.
Cheers
Christian
Hi Christian,
It’s been a while since I tested this, but I believe you would set the forum’s post ID in the shortcode like this:
[bbpress_recent_replies_by_topic show=10 forum=123]
Ok, thanks. Tried that but couldn’t get it to work.
Will try to debug the code and see what goes wrong.
Hi!
I was wondering if anyone could suggest how to use this function in a forum? I would like to show the latest replies above the categories in my forum at talanrien (dot) com/forums
Thank you!
I am trying to show a specific topic by ID and the 3 most recent published replies (excluding pending replies) underneath with a reply box.
How can I achieve this?
Many thanks for the usefull function. I see, that it accepts forum IDs, but it doesn’t seem to accept multiple IDs. Is there a way how to allow this?
thanks
I’ve figured it out with this code:
“`
if ($forum){
$args[‘post_parent’] = $forum;
$args[‘post_parent’] => ”,
}
“`
The downside is, that when your selected forums don’t have any topics, it will still show replies from the ones, which have been excluded by your selection. So there is only a custom query, but with no else statement for No posts.
This looks like it might do what I need, but I am trying to tweak it to fit.
I have a buddypress site with groups, and each group has a bbpress forum.
I want to list the recent posts for the forum IN THAT GROUP only when they visit the group page. So I want to assign the forum ID based on whichever group the user is in and is seeing the widget in.
Some questions:
1) How do I find out the forum id for the group’s forum?
2) How do I then assign that forum ID to to be the default forum ID for the widget in the code above?
Just in case anybody else stumbles on this and is looking for the solution. Here is my code for the first function to achieve what I did. Essentially, I hardcoded the forum to be the forum ID that was associated to that group, using bbp_get_group_forum_ids( $group_id )[0]. The function returns an array, so you need to use [0] to get the first/main forum id.
Hello,
How i can use multiple forums inside widget ?
I was try something like: [bbpress_recent_replies_by_topic show=10 forum=123,124,125] but it will not work .
Regards