Get Buddypress Group’s Forum ID and display topics by Group ID

As you may know, when you create a new Group in Buddypress, you can create also a Forum for the created Group. Today I want to show you my function to get the Buddypress Group's Forum ID by using Group ID in a custom page.

Here is my function:

function get_forum_id_by_group_id($group_id)
{
global $wpdb;
$forum_id = $wpdb->get_var("SELECT meta_value FROM wp_bp_groups_groupmeta WHERE group_id='$group_id' AND meta_key='forum_id'");
if($forum_id)
{
return $forum_id;
}
else
{
return false;
}
}

Now, I call the function:

get_forum_id_by_group_id($bp->groups->current_group->id)

$bp->groups->current_group->id you can use in Group's page or a group_id in other pages.

To display the topics:

if(get_forum_id_by_group_id($bp->groups->current_group->id))

{

$getId = unserialize(get_forum_id_by_group_id($bp->groups->current_group->id));
if(bbp_has_topics(array('post_parent'=>$getId[0]))):
while(bbp_topics()): bbp_the_topic();
bbp_get_template_part( 'loop', 'single-topic' );
endwhile;
endif;

}









Leave a Comment