BuddyDev

Search

Showing user sites on main site

Tagged: ,

  • Participant
    Level: Initiated
    Posts: 1
    Quincy Rossieau on #4794

    Hi,

    I am building a multisite setup on which I want to achieve the following:

    – One main site where all BuddyPress users of all subsites are displayed
    – Subsite 1, where only the BuddPress users of Subsite 1 are displayed
    – Subsite 2, where only the BuddPress users of Subsite 2 are displayed
    – Subsite 3, where only the BuddPress users of Subsite 3 are displayed
    – Subsite 4, where only the BuddPress users of Subsite 4 are displayed

    I have setup WP multisite and the BuddyPress Multi Network plugin, and I can create users in the subsites. They are also displayed on the main site. So that works.

    What I would like is to have the mainsite display the website a user is a member of. Currently the tab sites says 0.

    How can I get this to work?

  • Keymaster
    (BuddyDev Team)
    Posts: 21520
    Brajesh Singh on #4797

    Hi Quincy,
    Thank you for posting.

    You can get all the blogs of which the user is member by using this

    
    
    $blog_ids = BPNetworkUsers::get_networks( $user_id)
    
    

    It is not the most efficient method and It will be better to store the data in the user meta or do a bulk query instead of the above for individual users.

    Hope that helps.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 1
    Quincy Rossieau on #4803

    Hey Brajesh,

    Thanks for your answer.

    I’m not quite clear on how to go further now.

    How would I go about storing the data in user meta?

    Thanks,

    Quincy

  • Keymaster
    (BuddyDev Team)
    Posts: 21520
    Brajesh Singh on #4804

    Hi Quincy,
    You are welcome.

    The user meta approach will need me to add a few actions first. Also, I thought about it and since you only need it on the members listing page, The benefits may not outweigh the effor needed for synchronizing. So, Here is a better thing to do. Let us put it in our bp-custom.php

    
    
    function bd_cache_networks( $bp_user_query ) {
    
    	$user_ids = $bp_user_query->user_ids;//should be array of ids
    	$networks = array();//multi dimensional array of [user_id] =>[blog_id1, blog_id2]
    	if ( ! empty( $user_ids ) ) {
    		global $wpdb;
    		$table = mnetwork_get_table_name();;
    		$list = '(' . join( ',', $user_ids ) .')';
    
    		$user_networks = $wpdb->get_results( "SELECT user_id, network_id FRM {$table} WHERE user_id IN {$list}" );
    
    		foreach ( $user_networks as $unetwork ) {
    
    			if ( !isset( $networks[ $unetwork->user_id ] ) ) {
    				$networks[ $unetwork->user_id ] = array();//
    			}
    			//push the blog to user blogs list
    			$networks[$unetwork->user_id][] = $unetwork->network_id;
    		}
    
    	}
    
    	//store the current results
    	//not the best way, but will work fine
    	buddypress()->bdbp_networks_list = $networks;
    }
    add_action( 'bp_user_query_populate_extras', 'bd_cache_networks' )
    /**
     * Get an array of blog ids to which this user belongs to
     *
     * @return boolean|mixed
     */
    //now in members loop
    function bd_get_user_blog_ids() {
    	if ( isset( buddypress()->bdbp_networks_list ) ) {
    		return buddypress()->bdbp_networks_list[bp_get_member_user_id()];
    	}
    	return false;
    }
    
    

    now, calling bd_get_user_blog_ids() in the member loop will give you all the blog ids of which the user is a member.

    Hope that helps.

    • This reply was modified 6 years ago by Brajesh Singh. Reason: updated code with the missing table name

You must be logged in to reply to this topic.

This topic is: not resolved