Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Exclude everyone but moderators from bp_has_members in Buddypress WordPress

  • SOLVED

Notes to start from:

1. Working with Buddypress 1.5.1 and WordPress 3.2.1

2. Also using Justin Tadlock's Members plugin

3. Has to be something I can use in a particular context, not site-wide as I just need to show a list of the moderators on a particular page, not change the results of bp_has_members everywhere.

4. Unfortunately I can't give access as the site works with a vulnerable population

I'm trying to output the members of the site who have a user role of moderator onto a page.

Code I found suggests that this works to exclude 'subscriber' in the bp_has_members query:


<ul id="members-list" class="item-list">
<?php while ( bp_members() ) : bp_the_member();
$user = new WP_User( bp_get_member_user_id() );
if ( $user->roles[0] != 'subscriber' ) :
?>
<!--Member line item details and profile fields -->

<?php endif; ?>
<?php endwhile; ?>
</ul>


But I haven't been able to get it to only allow moderator.

Thanks very much.

Answers (2)

2011-12-05

Gabriel Reguly answers:

Hi cwulff,

How about this code


<ul id="members-list" class="item-list">
<?php while ( bp_members() ) : bp_the_member();
$user = new WP_User( bp_get_member_user_id() );
if ( $user->roles[0] == 'moderator' ) :
?>
<!--Member line item details and profile fields -->
<?php endif; ?>
<?php endwhile; ?>
</ul>


Regards,
Gabriel


Christopher comments:

That's interesting. It seems to be just showing nothing now if the record it returns isn't a moderator.

Do I just have it in the wrong place?


<section class="homewidget">
<header class="divider line"><h3>Featured Leader</h3></header>
<?php if ( bp_has_members('type=random&max=1') ) : ?>
<ul id="members-list" class="item-list">
<?php while ( bp_members() ) : bp_the_member();
$user = new WP_User( bp_get_member_user_id() );
if ( $user->roles[0] == 'moderator' ) :
?>
<li>
<div class="item-avatar">
<a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar('type=full&amp;width=100&amp;height=100') ?></a>
</div>

<div class="item">
<div class="item-title">
<h2 class="posttitle"><a href="<?php bp_member_permalink() ?>"><?php bp_member_name() ?></a></h2>
<?php if ( bp_get_member_latest_update() ) : ?>
<span class="update"> - <?php bp_member_latest_update( 'length=10' ) ?></span>
<?php endif; ?>
</div>
<!--<div class="item-meta"><span class="activity"><?php bp_member_last_active() ?></span></div>

<?php do_action( 'bp_directory_members_item' ) ?>-->

<?php

bp_member_profile_data( 'field=Organization' );
/***
* If you want to show specific profile fields here you can,
* but it'll add an extra query for each member in the loop
* (only one regadless of the number of fields you show):
*
* bp_member_profile_data( 'field=the field name' );
*/
?>
</div>

<!--
<div class="action">
<?php do_action( 'bp_directory_members_actions' ) ?>
</div>
-->

<div class="clear"></div>
</li>

<?php endif; ?>

<?php endwhile; ?>
</ul>

<?php endif; ?>

</section>


Christopher comments:

Oh, and thank you!


Gabriel Reguly comments:

Hi cwulff,

Does it works then?

I am glad to have helped, please do no forget to vote for me.

Have a good week.

Regards,
Gabriel


Christopher comments:

It works in that it doesn't show people who aren't moderators, but it just shows an empty block if the result of the query isn't a moderator rather than filtering them out of the query results.

Does the 'filter' need to be earlier in the query?


Gabriel Reguly comments:

Hi cwulff,

If you want to show something else if there are no moderators, then you will need to amend your code.


<ul id="members-list" class="item-list">

<?php while ( bp_members() ) : bp_the_member();

$user = new WP_User( bp_get_member_user_id() );

if ( $user->roles[0] == 'moderator' ) :
?>


<!--Member line item details and profile fields -->
<?php
else:
?>
<!--some other info -->

<?php endif; ?>

<?php endwhile; ?>

</ul>


Regards,
Gabriel


Gabriel Reguly comments:

Hi cwulff,

Another option is this


<section class="homewidget">
<header class="divider line">
<h3>Featured Leader</h3>
</header>
<?php if ( bp_has_members('type=random&max=1') ) :
$i = 0; ?>
<ul id="members-list" class="item-list">
<?php while ( bp_members() ) : bp_the_member();

$user = new WP_User( bp_get_member_user_id() );

if ( $user->roles[0] == 'moderator' ) :
$i++;

?>
<li>
<div class="item-avatar"> <a href="<?php bp_member_permalink() ?>">
<?php bp_member_avatar('type=full&amp;width=100&amp;height=100') ?>
</a> </div>
<div class="item">
<div class="item-title">
<h2 class="posttitle"><a href="<?php bp_member_permalink() ?>">
<?php bp_member_name() ?>
</a></h2>
<?php if ( bp_get_member_latest_update() ) : ?>
<span class="update"> -
<?php bp_member_latest_update( 'length=10' ) ?>
</span>
<?php endif; ?>
</div>
<!--<div class="item-meta"><span class="activity"><?php bp_member_last_active() ?></span></div>



<?php do_action( 'bp_directory_members_item' ) ?>-->
<?php



bp_member_profile_data( 'field=Organization' );

/***

* If you want to show specific profile fields here you can,

* but it'll add an extra query for each member in the loop

* (only one regadless of the number of fields you show):

*

* bp_member_profile_data( 'field=the field name' );

*/

?>
</div>
<!--

<div class="action">

<?php do_action( 'bp_directory_members_actions' ) ?>

</div>

-->
<div class="clear"></div>
</li>
<?php endif; ?>
<?php endwhile; ?>
<?php
if ( 0== $i ) {
?>
<li>There are no Moderators.</li>
<?php
}
?> </ul>
<?php endif; ?>
</section>


Regards,
Gabriel


Christopher comments:

Sorry, I wasn't clear. There are moderators. I refresh the page and it shows a moderator. I refresh again and another moderator.

But I refresh again and I get nothing. I think that it's still returning a result to the query (here's a member), but then when it realizes the result isn't a moderator it doesn't show it and instead of going back and finding a moderator to show it just shows nothing.


Gabriel Reguly comments:

Hi cwulff,

Ok, the reason this is happening is because you are getting one user at a time:


if ( bp_has_members('type=random&max=1') )


You will need to amend you code to get more users.

Let me do some research to see how to do it properly.

Regards,
Gabriel


Christopher comments:

Thanks. Buddypress has a few peculiarities in my limited experience with it so far.


Gabriel Reguly comments:

Hi cwulff,

Please try this code, it will show the first random moderator it finds.


<section class="homewidget">
<header class="divider line">
<h3>Featured Leader</h3>
</header>
<?php if ( bp_has_members('type=random') ) :
$i = 0; ?>
<ul id="members-list" class="item-list">
<?php while ( bp_members() ) : bp_the_member();

$user = new WP_User( bp_get_member_user_id() );

if ( $user->roles[0] == 'moderator' ) :
$i++;

?>
<li>
<div class="item-avatar"> <a href="<?php bp_member_permalink() ?>">
<?php bp_member_avatar('type=full&amp;width=100&amp;height=100') ?>
</a> </div>
<div class="item">
<div class="item-title">
<h2 class="posttitle"><a href="<?php bp_member_permalink() ?>">
<?php bp_member_name() ?>
</a></h2>
<?php if ( bp_get_member_latest_update() ) : ?>
<span class="update"> -
<?php bp_member_latest_update( 'length=10' ) ?>
</span>
<?php endif; ?>
</div>
<!--<div class="item-meta"><span class="activity"><?php bp_member_last_active() ?></span></div>



<?php do_action( 'bp_directory_members_item' ) ?>-->
<?php



bp_member_profile_data( 'field=Organization' );

/***

* If you want to show specific profile fields here you can,

* but it'll add an extra query for each member in the loop

* (only one regadless of the number of fields you show):

*

* bp_member_profile_data( 'field=the field name' );

*/

?>
</div>
<!--

<div class="action">

<?php do_action( 'bp_directory_members_actions' ) ?>

</div>

-->
<div class="clear"></div>
</li>
<?php endif; ?>
<?php if ( $i > 0 ) break; // exits the while loop ?>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</section>


Regards,
Gabriel


Christopher comments:

That works perfectly on the home page where it needs to return one leader.

How about on the interior page where it needs to return all and keep the count? I tried to implement your code as best as I could understand it and came up with this. But it has the same problem of returning empty <li> when a member isn't a moderator.

Thanks.



<div id="member-grid">

<?php $count = 0; if ( bp_has_members('type=random') ) : $i = 0; ?>

<div class="pagination">

<div class="pag-count" id="member-dir-count">
<?php bp_members_pagination_count() ?>
</div>

<div class="pagination-links" id="member-dir-pag">
<?php bp_members_pagination_links() ?>
</div>

</div>

<?php do_action( 'bp_before_directory_members_list' ) ?>

<ul id="members-list" class="item-list">

<?php while ( bp_members() ) : bp_the_member();
$user = new WP_User( bp_get_member_user_id() );
if ( $user->roles[0] == 'moderator' ) : $i++;
?>
<li class="<?php if ($count % 3 == 0) { echo "first"; } ?>">
<div class="item-avatar">
<a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar() ?></a>
</div>

<div class="item">
<div class="item-title">
<a href="<?php bp_member_permalink() ?>"><?php bp_member_name() ?></a>
<?php if ( bp_get_member_latest_update() ) : ?>
<span class="update"> - <?php bp_member_latest_update( 'length=10' ) ?></span>
<?php endif; ?>
</div>
<div class="item-meta"><span class="activity"><?php bp_member_last_active() ?></span></div>

<?php do_action( 'bp_directory_members_item' ) ?>

<?php
/***
* If you want to show specific profile fields here you can,
* but it'll add an extra query for each member in the loop
* (only one regadless of the number of fields you show):
*
* bp_member_profile_data( 'field=the field name' );
*/
?>
</div>

<div class="action">
<?php do_action( 'bp_directory_members_actions' ) ?>
</div>

<div class="clear"></div>
</li>

<?php endif; ?>

<?php
$count++;
endwhile;
?>
</ul>

<?php do_action( 'bp_after_directory_members_list' ) ?>

<?php bp_member_hidden_fields() ?>

<?php else: ?>

<div id="message" class="info">
<p><?php _e( "Sorry, no members were found.", 'buddypress' ) ?></p>
</div>

<?php endif; ?>

</div>




Gabriel Reguly comments:

Hi cwulff,

I am afraid that is extending the question a little too much...

But anyways, please try this code


<div id="member-grid">
<?php $count = 0; $i = 0; if ( bp_has_members('type=random') ) : ?>
<div class="pagination">
<div class="pag-count" id="member-dir-count">
<?php bp_members_pagination_count() ?>
</div>
<div class="pagination-links" id="member-dir-pag">
<?php bp_members_pagination_links() ?>
</div>
</div>
<?php do_action( 'bp_before_directory_members_list' ) ?>
<ul id="members-list" class="item-list">
<?php while ( bp_members() ) : bp_the_member();

$user = new WP_User( bp_get_member_user_id() );

if ( $user->roles[0] == 'moderator' ) : $i++;

?>
<li class="<?php if ($count % 3 == 0) { echo "first"; } ?>">
<div class="item-avatar"> <a href="<?php bp_member_permalink() ?>">
<?php bp_member_avatar() ?>
</a> </div>
<div class="item">
<div class="item-title"> <a href="<?php bp_member_permalink() ?>">
<?php bp_member_name() ?>
</a>
<?php if ( bp_get_member_latest_update() ) : ?>
<span class="update"> -
<?php bp_member_latest_update( 'length=10' ) ?>
</span>
<?php endif; ?>
</div>
<div class="item-meta"><span class="activity">
<?php bp_member_last_active() ?>
</span></div>
<?php do_action( 'bp_directory_members_item' ) ?>
<?php

/***

* If you want to show specific profile fields here you can,

* but it'll add an extra query for each member in the loop

* (only one regadless of the number of fields you show):

*

* bp_member_profile_data( 'field=the field name' );

*/

?>
</div>
<div class="action">
<?php do_action( 'bp_directory_members_actions' ) ?>
</div>
<div class="clear"></div>
</li>
<?php endif; ?>
<?php
$count++;
endwhile;
?>
</ul>
<?php do_action( 'bp_after_directory_members_list' ) ?>
<?php bp_member_hidden_fields() ?>
<?php endif; ?>
<?php if ( 0 == $i : ?>
<div id="message" class="info">
<p>
<?php _e( "Sorry, no members were found.", 'buddypress' ) ?>
</p>
</div>
<?php endif; ?>
</div>


Regards,
Gabriel


Christopher comments:

Hi Gabriel,

Sorry about that. Meant to increase it last night before I went to bed. I appreciate your effort very much.

Unfortunately, this last code didn't work. It returns the error:

Parse error: syntax error, unexpected ':' on line 166 which is

<?php if ( 0 == $i : ?>


Christopher comments:

I added the ) to close it, so now I don't get the error.

But it still says it's showing the first twenty members, when in fact it's only showing 15 (it's still including the admins and subscribers in the query I suspect since it's throwing off the 'count' too).

Thoughts?


Gabriel Reguly comments:

Hi cwulff,

Sorry about the error, gladly you fixed it.

Seems to me that function bp_members_pagination_count() is the culprit.

Let me research how it works.

Regards,
Gabriel


Christopher comments:

I commented out that section and it doesn't seem to have made a difference.


Gabriel Reguly comments:

Hi cwulff,

Really?

Well, then we need to find the source of the text you want to change.

Can we schedule a Skype conversation? I'll send you a PM.

Regards,
Gabriel

2011-12-05

Linda answers:

Hi, I used this plugin in my install with Justin Tadlock's Members plugin to show a list of my members. Maybe it would work for you?

[[LINK href="http://wpusersplugin.com/"]]http://wpusersplugin.com/[[/LINK]]


Christopher comments:

Thank Linda. I appreciate the response but it's not what I'm looking for.