Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daggerhart/4244c147f47a6bd95255 to your computer and use it in GitHub Desktop.
Save daggerhart/4244c147f47a6bd95255 to your computer and use it in GitHub Desktop.
Custom bbpressRecent replies shortcode - optimized.
<?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), $atts);
extract($short_array);
// get the 5 topics with the most recent replies
global $wpdb;
$reply_ids = $wpdb->get_col($wpdb->prepare("SELECT pm.meta_value FROM {$wpdb->postmeta} as pm LEFT JOIN {$wpdb->posts} as p on pm.post_id = p.ID where p.post_type = 'topic' AND p.post_status = 'publish' AND pm.meta_key = '_bbp_last_reply_id' order by pm.meta_value * 1 DESC LIMIT %d", $show));
// get the actual replies themselves
$args = array(
'posts_per_page' => $show,
'post_type' => array('reply'),
'post_status' => 'publish',
'post__in' => $reply_ids,
'orderby' => 'date',
'order' => 'DESC',
// performance
'no_found_rows' => true,
'cache_results' => false,
);
$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
*/
/*
* 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');
@daggerhart
Copy link
Author

@Reghyz
Copy link

Reghyz commented Mar 4, 2015

hi, thanks for this piece of code,
do you know how i can exclude forums by their _bbp_forum_id from this querry ?

thanks

@Reghyz
Copy link

Reghyz commented Mar 4, 2015

oh ok , I find it out by myself (i include forums id and dont exclude them finally) : :

$forumsID = '[123,124,125]'; // or for a single forum : $forumsID =123
$short_array = shortcode_atts(array('show' => 5, 'forum' => $forumsID, 'include_empty_topics' => false), $atts);

@Reghyz
Copy link

Reghyz commented Mar 4, 2015

ah... i spooke to fast , this code dont work, it's the sames with the var set on false

@alexkappel
Copy link

if ( get_the_author() ) {
$bbp_author = get_the_author();
}
else {
$bbp_author = get_post_meta( get_the_ID(), '_bbp_anonymous_name', true);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment