Ali Jafarian

bbPress is the default WordPress forum plugin, which give it some nice compatibility with the WP core.

Today we’re going to take a quick look at how to query bbPress topics in your WordPress template files. To do this we’ll use the get_posts() function and a foreach loop. We’ll render our list of topics using a standard ul.

<ul class="topics">

	<?php
		$latest_topics = get_posts( array(
		    'post_type' => 'topic',
		    'posts_per_page' => 3
		) );
		foreach ( $latest_topics as $post ) : setup_postdata( $post );
	?>

		<li><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li>

	<?php endforeach; ?>
	
	<?php wp_reset_postdata(); ?>
	
</ul>

NOTE: You’ll also notice that I have the wp_reset_postdata() function after my query. This is needed so WordPress can “reset” the query for future use.

Conclusion

As you can see, querying bbPress topics is just like querying WordPress posts, since WordPress stores topics as their own post_type. This gives you a lot of flexibility in how you query and render your bbPress topics. I hope this was helpful in your projects!

Discussion

Leave a Comment

Your email address will not be published. Required fields are marked *