In this quick tip, we will try to get the total bbPress favorites made for all topics by a specific user, counting total favorites received on every topic this user has posted on the forums.

As if you don’t know, there’s this function bbp_get_topic_favoriters( int $post_ID ) that you can call to get an array of users who have favorite’d this topic we are specifying its POST ID in the first param. That is what we will use to count total bbPress favorites on all topics as we query topics by user ID who is the author of these topics.

We will use WordPress API get_posts to list all topics of the user we’re after.

function se_bbp_total_favorites_by_user( $user_id ) {
	$args = array( 'post_type' => array('topic'), 'author' => $user_id, 'posts_per_page' => -1 );
	$posts = get_posts( $args );
	$count = 0;

	if ( !empty( $posts ) ) {
		foreach ( $posts as $post ) {
			$count += count( bbp_get_topic_favoriters( $post->ID ) );

	return (int) $count;

Good, now simply call se_bbp_total_favorites_by_user( 1 ) with the user ID instead of 1 and it should be working perfect.

Caching bbPress favorites:

Please note that you should not always make query to the posts while you can do it in the first time and cache data for a later use with custom expiration interval. Caching is always the best tool out there to minimize the load time, and reduce memory and server ressource usage, and because this is simple enough, let’s just use WordPress transients for non-persistent caching, and set the timeout to a week or 2 days (DAY_IN_SECONDS * 2) or just flush every time a topic gets favorited (I don’t know how to hook into this, guess you’ll have to look into the core files for a custom hook)

Getting All Favorited Topics By User:

I have looked around the usermeta table in PMA of a local install, and I found out that all topics favorites by a user are saved in a custom user meta with the name of wp__bbp_favorites, saved in a comma-separated format. This can be used to list all topics that this user has favorited, as we’ll just look for that meta, explode the commas and we’re all set with the topics IDs.

function se_bbp_favorites_by_user( $user_id ) {
	$meta = get_user_meta( $user_id, "wp__bbp_favorites", 1 );
	if ( $meta ) {
		$meta = explode( ",", $meta );
		$meta = array_filter( $meta );
		if ( !empty( $meta ) ) {
			foreach ( $meta as $i => $topic_id ) {
				$meta[$i] = (int) $topic_id;
				* you might want to check if this post exists
				* or instead of attaching post ID only, make it post data
				* as $meta[$i] = get_post( $topic_id );
	} else return array();
	return $meta;

For this one, no need to worry about the caching (as long as you keep it simple as is) because by default, WordPress caches the user meta and loads it in memory; the cache will automatically be flushed when the user meta is updated (cool stuff right? WordPress!)

Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit