Log in
Home  »  Website Management Tips & Services   »   Display Related Posts or Topics in bbPress without Plugin

Display Related Posts or Topics in bbPress without Plugin

Published on August 2nd, 2023 | Last updated on August 6th, 2023 by | No Comments on Display Related Posts or Topics in bbPress without Plugin | 372 Views | Reading Time: 11 minutes

Have you ever wanted to display related posts or topics in bbPress like the way you show them in WordPress posts and pages? bbPress is technically the best WordPress plugin to add forum functionality to your WordPress website. However, this plugin does not show any related posts or topics by default. In this article, you will learn how to show related topics in bbPress without any plugin.

Watch this video on how to display related posts or topics in bbPress

Create forums with bbPress

How to display related posts or topics in bbPress forums

bbPress is the best WordPress plugin to add forum functionality to your website. Even wordpress.org is using bbPress to create an active community and support WordPress users with forums. If you need to run forums on your website to create an active and inviting community, we highly recommend you install bbPress WordPress plugin.

Why display related posts in bbPress topics?

One of the problems with bbPress is that it has not been updated for around two years. In fact, this plugin has a minimalistic design, and you need to install some additional WordPress plugins to extend its functionality, such as related posts or topics. However, any added plugin can slow down your WordPress website, and therefore, it’s not logical to install so many plugins.

Related Lessons at the bottom of each lessons at LELB Society
How to display related posts or lessons at the bottom of each WordPress post

At LELB Society, which is a bilingual academy of English and Persian with over 220 registered students and members, it is important for us to display related posts or topics in our forums. This is because asynchronous learning and supporting our students 24/7 are among our main services.

Research has proved that displaying related posts in any WordPress custom post type, such as posts, pages, bbPress topics, etc. can increase user engagement and pageviews. As a result, you can significantly improve the SEO ranking of your website. In this way, your users can stay longer on your website and potentially become your customers or subscribers.

PHP Code to display related posts in bbPress

Show related posts in WordPress without plugin
How to display related posts or topics in bbPress without any plugin to increase conversion rate

The following php code is completely optimized and ready to be used. This code will query and fetch 5 related topics that are automatically selected according to the topic tags that exist in any topic. Therefore, for this php code to function properly, first you need to enable topic tags in bbPress. To enable topic tags, in your WordPress dashboard area, go to Settings, Forums, and in the Topic tags section, select Allow topics to have tags.

function aa_related_topics() {
        $topic_id = bbp_get_topic_id();
        $tags = wp_get_post_terms( $topic_id, bbp_get_topic_tag_tax_id(), array( 'fields' => 'ids' ) );
        $args = array(
                'post__not_in' => array($topic_id),
                'post_type'  => bbp_get_topic_post_type(),
                'posts_per_page' => 5,
                'orderby'   => 'DESC',
                'tax_query' => array(
                        array(
                                'taxonomy' => bbp_get_topic_tag_tax_id(),
                                'terms'    => $tags,
                        ),
                ),
        );
        $my_query = new wp_query( $args );
        if( $my_query->have_posts() ) {
            echo '<div id="related"><h2>Related Topics in Forums</h2>';
                while( $my_query->have_posts() ) {
                    $my_query->the_post(); ?>
                    <div>
						<h3><a href="<?php the_permalink()?>" title="<?php the_title(); ?>"><p><?php the_title(); ?></p><?php the_post_thumbnail(); ?></a></h3>					
						<p><?php the_time('M j, Y') ?></p>			
                    </div>
                <?php }
                wp_reset_postdata();
            echo '</div>';
        }
}
add_action( 'bbp_template_after_single_topic', 'aa_related_topics' );

Topic tags for forums act like tags for posts, and they can come in handy especially when you have a great number of topics and you need to organize them. You can also read this article to show related posts in WordPress posts without any plugin.

Where to put the above php code?

English language forum to ask English questions and receive immediate responses
How to display related posts in WordPress posts and topic in bbPress forums

Contrary to popular belief, you don’t need to add this code to your functions.php file. This is because you don’t need to run this code on any page or post. A more rational solution would be to add this code to the end of the template.php file of bbPress topics which is retrievable in the following path when you go to Plugins, Plugin File Editor in your WordPress dashboard area, and select bbPress from the Select plugin to edit drop-down menu on the top-right corner of your screen:

Editing bbpress/includes/topics/template.php (active)

Please note that any imperfection in your plugins or theme files could lead to fatal errors. So, it’s wise to create a backup of your site before editing these files.

How to tweak the php code to display related posts?

Professional Website Development & Design Service for Successful Online Business at LELB Society
Tweaking the php code that can display related posts and topics

The following are some changes you could make to the php code in order to optimize or personalize it the way you like it to be:

  •  ‘posts_per_page’ => 5, = to modify the number of related posts or topics in forums
  • ‘orderby’ => ‘DESC’, = to set descending order to display related posts in bbPress or choose “ASC” for ascending order or “rand” for random selection
  • echo ‘<div id=”related”><h2>Related Topics in Forums</h2>’; = To choose a title for this element on your website, the original title is “Related Topics for Forums”.
  • <p><?php the_time(‘M j, Y’) ?></p> = This line of code adds the date stamp right below each related topic and you can remove this line if you don’t like to show date stamps.
  • <?php the_post_thumbnail(); ?> = This piece of code will fetch and query the post thumbnail or featured image of any selected topic.

Add a featured image for any forum, topic or reply

With the above code, you can display related posts in topics with post thumbnails or featured images. Showing related posts with featured images can grab more attention and increase user engagement. But the main problem is that bbPress does not support post thumbnails for forums, topics or replies. To achieve this, you can simply add the following three lines of code to the end of your functions.php file:

add_post_type_support('forum', array('thumbnail'));
add_post_type_support('topic', array('thumbnail'));
add_post_type_support('reply', array('thumbnail'));

14 English and Persian forums at LELB Society

Create a new topic in our English and Persian forums to ask us your questions or express your opinions
14 English and Persian forums at LELB Society with related posts in each topic

Below, you can see the list of our 14 English and Persian forums available to our registered students. Our students can easily create a topic in our forums and post their questions or opinions. The team of our educators, including Dr. Mohammad Hossein Hariri Asl, the founder of LELB Society on WordPress and the author of this article, would respond to all created topics immediately and in detail. 

Explore our related posts for each topic

Join our forums at LELB Society.
Showing related posts at the bottom of each topic in forums

You can check how we’ve been able to display related posts for each single topic in our forums. In the following list, you can browse 15 new topics in our forums. By selecting any one of these topics, you can check related posts at the bottom of each topic.

Viewing 15 topics - 1 through 15 (of 209 total)
Viewing 15 topics - 1 through 15 (of 209 total)

Include bbPress forums, topics and replies in search results

Use the search box on the main menu to search for anything.
Include bbPress forums, topics and replies with featured images in WordPress search results

Now that your topics and forums are enriched with featured images, why not include them in WordPress search results? As you might already know, bbPress has got its own search box as displayed below applicable only to LELB Society’s forums:

The following php code can include your forums, topics and replies in the default WordPress search box, which is to be found on the main menu at LELB Society as illustrated in the above image. Simply copy and paste this php code in your functions. php file.

function ntwb_bbp_topic_cpt_search( $topic_search ) {
	$topic_search['exclude_from_search'] = false;
	return $topic_search;
}
add_filter( 'bbp_register_topic_post_type', 'ntwb_bbp_topic_cpt_search' );
function ntwb_bbp_forum_cpt_search( $forum_search ) {
	$forum_search['exclude_from_search'] = false;
	return $forum_search;
}
add_filter( 'bbp_register_forum_post_type', 'ntwb_bbp_forum_cpt_search' );
function ntwb_bbp_reply_cpt_search( $reply_search ) {
	$reply_search['exclude_from_search'] = false;
	return $reply_search;
}
add_filter( 'bbp_register_reply_post_type', 'ntwb_bbp_reply_cpt_search' );

Make bbPress even more SEO friendly

verse LELB Society
How to add description for single forums in bbPress

Another thing you can do to improve and optimize bbPress for SEO purposes is to add description for each forum on the main forums list as shown above in LELB Society’s forums list. This is also possible by adding the following php code to the end of your functions.php file. Without this code, the description you provide for a forum is not displayed in the forums list.

function rw_singleforum_description() {
  echo bbp_forum_content();
}
add_action( 'bbp_template_before_single_forum' , 'rw_singleforum_description');

About Dr. Mohammad Hossein Hariri Asl

Dr. Mohammad Hossein Hariri Asl is an English and Persian instructor, researcher, inventor, author, blogger, SEO expert, website developer, and the creator of LELB Society. He's got a PhD in TEFL (Teaching English as a Foreign Language). Study our guest posting guidelines for authors.

View All 3983 Posts by this Author »

We respond to all comments immediately. View the 30 newest comments and new topics in forums.

Leave a Comment

one × four =