Skip to:
Content
Pages
Categories
Search
Top
Bottom

bp_get_member_type() is not working in the loop


  • Philipp
    Participant

    @philippmuenchen

    Hi @ all!

    I was so happy to see this brilliant new feature in BP 2.2 as I lived with my own solution for the past years what did everything a bit complicated. So I was happy to register two new member types and to assign some types to different members. Now I would like to show in the members loop, what member type the single members are. But this is no working. I tried it for the past hours in many different ways without success. Please have a look to my code below, which I used within the members-loop.

    BTW if I use bp_get_member_type( '123' ) with the ID of a single user, I gives me the result. But that’s not what I need.

    <?php 
    $user_id = bp_get_member_user_id();
    $type = bp_get_member_type( $user_id );
    ?>

    I’m looking forward to your answers,

    Philipp

Viewing 23 replies - 1 through 23 (of 23 total)

  • Henry Wright
    Moderator

    @henrywright

    Hi @philippmuenchen

    That’s strange. bp_get_member_user_id() will get you the member’s ID if it’s used within the loop. Your code looks sound to me so I’m puzzled as to why this isn’t working.


    Philipp
    Participant

    @philippmuenchen

    Thanks for your reply. It’s absolutely weird and I wasn’t able to find the fix. Might this happen if the function is loaded more then once?

    Maybe you’re able to reproduce what I did. If it’s working with someone else it might be another issue with my install. But in general it’s working fine if I hardcode the ID. That’s why I’m confused.

    Best,

    Philipp


    Henry Wright
    Moderator

    @henrywright

    The fact that hard-coding the user ID works indicates the problem is with bp_get_member_user_id(). Can you echo it to screen to see what value it returns? And sorry to double-check but are you sure you’re using it within the members loop?


    shanebp
    Moderator

    @shanebp

    BTW if I use bp_get_member_type( ‘123’ )

    Strange that it would accept a string.
    Please also try with an integer: bp_get_member_type( 123 )


    Philipp
    Participant

    @philippmuenchen

    Hi and thanks for your answers.


    @henrywright
    : If I echo bp_get_member_user_id() it returns the correct ID for each user. And yes, I’m in the members-loop.


    @shanebp
    : It accepts both with ” and without.

    Best regards,

    Philipp


    danbp
    Moderator

    @danbp

    hi @philippmuenchen,

    here’s a gist published by @imath to sort the member directory by member_type.

    That (working)example is based on this Codex article


    Henry Wright
    Moderator

    @henrywright

    In that case, your code should work. There must be something weird happening causing it to not work.


    Henry Wright
    Moderator

    @henrywright

    Out of interest, can you try echo bp_get_member_type( bp_get_member_user_id() );

    Perhaps there’s a weird character in the code you’ve written. That’s all I can think of?


    Philipp
    Participant

    @philippmuenchen

    Hi @ all,

    still it’s not working. Also if I echo bp_get_member_type( bp_get_member_user_id() );.
    Here’s my code again to show that I’m in the loop:

    <?php do_action( 'bp_before_directory_members_list' ); ?>
    
    	<ul id="members-list" class="item-list" role="main">
    
    	<?php while ( bp_members() ) : bp_the_member(); ?>
    
    		<?php 
    		$user_id = bp_get_member_user_id();
    		$type = bp_get_member_type( $user_id );
    		?>

    I also tried it with global $bp; and global $wpdb; but without success.

    BTW the code by @imath is working great.


    Henry Wright
    Moderator

    @henrywright

    Thanks for providing your code. Are you printing $type to screen? What do you get when you do echo $type;?


    Philipp
    Participant

    @philippmuenchen

    I set up two types (‘kw’ & ‘max’) so what i want to achieve is that the echo $type; prints ‘kw’ or ‘max’ depending on what the user is.


    Henry Wright
    Moderator

    @henrywright

    I set up two types (‘kw’ & ‘max’) so what i want to achieve is that the echo $type; prints ‘kw’ or ‘max’ depending on what the user is.

    That’s great, but what does echo $type actually print to screen? For example:

    <?php do_action( 'bp_before_directory_members_list' ); ?>
    
    	<ul id="members-list" class="item-list" role="main">
    
    	<?php while ( bp_members() ) : bp_the_member(); ?>
    
    		<?php 
    		$user_id = bp_get_member_user_id();
    		$type = bp_get_member_type( $user_id );
                    echo $type;
    		?>

    Philipp
    Participant

    @philippmuenchen

    🙂 Sorry, I got you wrong. It simply displays nothing. It’s blank.


    Henry Wright
    Moderator

    @henrywright

    Or better still, what does var_dump( $type ); show you?


    Philipp
    Participant

    @philippmuenchen

    For some its NULL and for others bool(false).


    Henry Wright
    Moderator

    @henrywright

    Thanks. And, just to check, are you running BP version 2.2?


    Henry Wright
    Moderator

    @henrywright

    Can you try clearing your cache?


    Philipp
    Participant

    @philippmuenchen

    Thanks! Yes I do use BP 2.2.

    I just used another Browser and cleared the cache. Nothing changed.

    Hi @philippmuenchen

    Thanks for your feedback about the gist 🙂

    I’m on your issue. I think i’ve found what’s going wrong. Checking and will be back in a few if it’s confirmed.

    @philippmuenchen @henrywright

    Ok got it.

    First i’ve just created this ticket and suggested a patch to solve the issue.

    Now, you can still have it working by simply requesting a ‘not single’ member type using false as an extra argument eg :
    bp_get_member_type( bp_get_member_user_id(), false );

    So you’ll get the member type name, but not the singular name. If you want to have the singular name, here’s another gist :
    https://gist.github.com/imath/3a5c5ade44eae997fdfd


    Henry Wright
    Moderator

    @henrywright

    Hi @imath

    Thanks for investigating! Who’d have thought array_pop() needing an array instead of a string would be the culprit. You’ve made sense of a very puzzling problem 😀


    Philipp
    Participant

    @philippmuenchen

    Yes! And he made me to a lucky man. 😉 Special thanks to @imath and @henrywright for your help and support. It’s working as expected now.

    Best!

    You’re welcome 🙂 BuddyPress 2.2 is awesome !

Viewing 23 replies - 1 through 23 (of 23 total)
  • The topic ‘bp_get_member_type() is not working in the loop’ is closed to new replies.
Skip to toolbar