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

Recent Author Post in Sidebar, How to exclude certain users? WordPress

  • SOLVED

Hi, and thanks for your kind support.

I want to list last post by user in my sidebar, with author name (link to profile), and avatar.
I want to exclude admin and other users, but can´t seem to be able to
I am using this code:


<?php
//get all users, iterate through users, query for one post for the user,
//if there is a post then display the post title, author, content info
$blogusers = get_users_of_blog();

if ($blogusers) {
foreach ($blogusers as $bloguser) {
$args = array(
'author' => $bloguser->user_id,
'showposts' => 1,
'caller_get_posts' => 1,
);

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>

<div class="bloglist">
<div class="blogavatar">
<?php echo get_avatar( get_the_author_email(), '30' ); ?>
</div>
<div class="blogtitle">
<h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a><br /><small>Por <?php the_author_posts_link(); ?> </small></h4>
<!--<?php the_excerpt();?>
<p class="more-link"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" style="color: rgb(157, 20, 20); font-weight: bold;"><?php _e('Read', 'news'); ?></a> | Comments (<?php comments_number('', '1', '%'); ?>)</p>-->
</div>
</div>
<? endwhile;
}

}
}

?>



Also, can u show me how to put this into a function that I can call from the sidebar.php instead of writing the whole code each time?

Answers (3)

2010-10-13

Jarret Minkler answers:

foreach ($blogusers as $bloguser) {
// Either array of id's or usernames
$exclude_ids = array(
'10' => true,
'11' => true,
'12' => true,
'12' => true,
);

// If using username replace the ->user_id
if(isset($exclude_ids[$bloguser->user_id])) {
continue;
}

$args = array(

'author' => $bloguser->user_id,

'showposts' => 1,

'caller_get_posts' => 1,

);


Luciano comments:

Sorry for being dumb, but as stated in the original question, can u show me how to put this into a function that I can call from the sidebar.php instead of writing the whole code each time I need to?

Thxs!


Jarret Minkler comments:

@Jimish is same solution as mine ..

You just copy the lines that i wrote there the same place you cut them from, why would you need to paste it over again?


Jarret Minkler comments:

<?php



function blogAuthors() {




//get all users, iterate through users, query for one post for the user,

//if there is a post then display the post title, author, content info

$blogusers = get_users_of_blog();



if ($blogusers) {

foreach ($blogusers as $bloguser) {

$args = array(

'author' => $bloguser->user_id,

'showposts' => 1,

'caller_get_posts' => 1,

);



$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

while ($my_query->have_posts()) : $my_query->the_post(); ?>



<div class="bloglist">

<div class="blogavatar">

<?php echo get_avatar( get_the_author_email(), '30' ); ?>

</div>

<div class="blogtitle">

<h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a><br /><small>Por <?php the_author_posts_link(); ?> </small></h4>

<!--<?php the_excerpt();?>

<p class="more-link"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" style="color: rgb(157, 20, 20); font-weight: bold;"><?php _e('Read', 'news'); ?></a> | Comments (<?php comments_number('', '1', '%'); ?>)</p>-->

</div>

</div>

<? endwhile;

}



}

}




} // End blogAuthors




?>


put it in functions.php without the <?php ?> etc


Luciano comments:

U got the prize, thxs for the answer...
I wanted a solution to make the code lighter, that´s why I wanted to include it in the functions.php.

How do I call it from sidebar.php once is included in functions?

2010-10-13

Matt Zeisler answers:

---Edited---
Or I could remember that's a WP function that has no parameters.

2010-10-14

Jimish Gamit answers:

<strong>$exclude_users_id = array(2,3);
//let suppose admin id is 2 and 3 for some other user which u want to disable</strong>
foreach ($blogusers as $bloguser) {

<strong> if(in_array($bloguser->user_id,$exclude_users_id))
continue;
// This will not execute below code when condition matched and move to next record</strong>
$args = array(

'author' => $bloguser->user_id,

'showposts' => 1,

'caller_get_posts' => 1,

);