I have a menu that lists authors in a drop down. The list is being output by the two functions below. I need a way to control the amount of authors seen, order by post counts and most importantly, filter by role. Obviously I'd like to be able to set these parameters in the function calls rather than code them n the function itself so I can reuse the functional call when needed elsewhere.
So the question is: Can someone alter these functions to allow the enhanced capabilities above?
function menu_experts(){
$temp_authors = custom_list_menu_authors(array('echo' => false));
$t_authors = explode('[separator]', $temp_authors);
$count = count($t_authors) - 1;
$show = '';
$i = 0;
foreach($t_authors as $author){
if($i % 9 == 0 && $i != $count && $i != 0)
$show .= "</ul>\n<ul>\n";
$show .= $author;
$i++;
}
$temp = '<div class="drop drop02">
<div class="drop-holder">
<div class="holder">
<div class="info-box">
<div class="hold">
<ul>
' . $show . '
</ul>
</div>
</div>
</div>
<span class="b">
<span class="l"> </span>
<span class="r"> </span>
</span>
</div>
</div>';
return $temp;
}
function custom_list_menu_authors($args = '') {
global $wpdb;
$defaults = array(
'orderby' => 'name', 'order' => 'ASC', 'number' => '',
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
$return = '';
$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
$query_args['fields'] = 'ids';
$authors = get_users( $query_args );
$author_count = array();
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
$author_count[$row->post_author] = $row->count;
$temp = array();
foreach ($authors as $author_id ) {
$author = get_userdata( $author_id );
if ( $exclude_admin && 'admin' == $author->display_name )
continue;
$posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0;
if ( !$posts && $hide_empty )
continue;
$link = '';
if ( $show_fullname && $author->first_name && $author->last_name )
$name = "$author->first_name $author->last_name";
else
$name = $author->display_name;
if ( !$html ) {
$return .= $name . ', ';
continue; // No need to go further to process HTML.
}
if ( 'list' == $style ) {
$return .= '<li>';
}
$link = "<div class=\"image\">\n";
$link .= '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . get_avatar( $author->ID, 23 ) . "</a>\n";
$link .= "</div>\n";
$link .= "<div class=\"txt\">\n";
$link .= '<strong><a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '">' . $name . "</a></strong>\n";
$link .= '<span>' . get_the_author_meta('department', $author->ID) . "</span>\n";
$link .= "</div>\n";
if ( !empty( $feed_image ) || !empty( $feed ) ) {
$link .= ' ';
if ( empty( $feed_image ) ) {
$link .= '(';
}
$link .= '<a href="' . get_author_feed_link( $author->ID ) . '"';
$alt = $title = '';
if ( !empty( $feed ) ) {
$title = ' title="' . esc_attr( $feed ) . '"';
$alt = ' alt="' . esc_attr( $feed ) . '"';
$name = $feed;
$link .= $title;
}
$link .= '>';
if ( !empty( $feed_image ) )
$link .= '<img src="' . esc_url( $feed_image ) . '" style="border: none;"' . $alt . $title . ' />';
else
$link .= $name;
$link .= '</a>';
if ( empty( $feed_image ) )
$link .= ')';
}
if ( $optioncount )
$link .= ' ('. $posts . ')';
$return .= $link;
$return .= ( 'list' == $style ) ? "</li>\n[separator]" : ', ';
}
$return = rtrim($return, ', ');
if ( !$echo )
return $return;
echo $return;
}
Arnav Joy answers:
change following line
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
$author_count[$row->post_author] = $row->count;
with
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS nbFROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author ORDER BY nb DESC") as $row )
$author_count[$row->post_author] = $row->nb;
Arnav Joy comments:
also for the role based filter use my ans.
http://wpquestions.com/question/showLoggedIn/id/4259
dkraljic comments:
Hi,
I replaced the code you referenced but now the menu is not showing anything. I do see that you referenced a previous answer. Which leads me to thin you expect <?php custom_list_authors($args ,'administrator'); ?> to be used? in this case the output is in the header. In the header file there is the following code so I don't think I can use a one line call like that:
<?php wp_nav_menu( array('container' => false,
'theme_location' => 'primary',
'menu_id' => 'nav',
'menu_class' => 'navigation',
'depth' => 2,
'items_wrap' => '<ul id="%1$s">%3$s</ul>',
'walker' => new New_Walker_Nav_Menu) ); ?>
I guess the functions I pasted above are being called by the custom walker but I am not rally sure how all this is tying together.
Sabby Sam answers:
Hi,
I am just confuse from the above code but I am giving you one quick solution which is given below :
This is query to fetch
<blockquote>" I need a way to control the amount of authors seen, order by post counts and most importantly "</blockquote>
$get_results="SELECT count(p.post_author) as post1,c.id, c.user_login, c.display_name, c.user_email, c.user_url, c.user_registered FROM {$table_prefix} as c , {$table_prefix1} as p {$where} and p.post_type = 'post' AND p.post_status = 'publish' and c.id=p.post_author GROUP BY p.post_author order by post1 DESC limit {$author_numbers} ";
You can check this code running on this site http://blog.radisys.com/
on the sidebar there is author widget, and the list is generated order by post, you can see this by mouseover the author.
If you want more explanation then visit the below links.
http://wordpress.org/extend/plugins/author-profiles/
or use this one
http://webinsolution.com/plugins/wordpress-author-profile-avatars
If you don't understand than drop an email me at [email protected], this above plugin is developed by me.
I hope this will help you.
dkraljic comments:
sabby,
I am not using this in a widget area. It is in a menu.
dkraljic comments:
sorrry, should have ben more explicit. The final output is rendered by this in the header file:
<?php wp_nav_menu( array('container' => false,
'theme_location' => 'primary',
'menu_id' => 'nav',
'menu_class' => 'navigation',
'depth' => 2,
'items_wrap' => '<ul id="%1$s">%3$s</ul>',
'walker' => new New_Walker_Nav_Menu) ); ?>
Sabby Sam comments:
Hi,
Insted of this
$author_count = array();
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
$author_count[$row->post_author] = $row->count;
You can use this one
$author_count = array();
$get_results="SELECT count(p.post_author) as post1,c.id, c.user_login, c.display_name, c.user_email, c.user_url, c.user_registered FROM {$table_prefix} as c , {$table_prefix1} as p {$where} and p.post_type = 'post' AND p.post_status = 'publish' and c.id=p.post_author GROUP BY p.post_author order by post1 DESC limit {$author_numbers} ";
$author_count=(array) $wpdb->get_results("{$get_results}", object);
<strong>Kindly chnage $table_prefix with you desire name ( $wpdb->posts )</strong>
after this you can use this as
foreach ( $author_count as $count ) {
$user = get_userdata($count->id);
echo get_avatar( $user->user_email, $size = $author_size);
echo $user->user_login;
}
I hope this above example is just for explanation and you can modify this. If you can provide the actual login or ftp details to me on [email protected] then I would solve it easily.
dkraljic comments:
Sabby,
Sorry, but I cannot trust someone I met on the internet with ftp or wp login access. It just ins't safe business practice. I'm sure you understand.
The original code I posted above has all the items needed to write the code without me having to replace $table_prefix myself. Or at least I think it does. Does it not? Or are you saying that $wpdb->posts is what I should use? Finally, you haven't explained how I would later alter the role I want to filter which is part of my original question.
THanks,
D
Sabby Sam comments:
Okay,
The above is code is just for explanation and without integrating this code in the file no one can say the output. Do the following things
1. Hire some one and sort the issue. Or
2. Try your self with my code its work 100% you can see the working on more than 100 site.
$table_prefix ( <em>this is used in my code</em> ) this is just a query (<strong> it is example I gave you understand </strong>).
If you still having some problem then Kindly mail me the copy of the your theme file, I am sure you understand thinking the code is different and implementing the code is different.
I hope you understand this.