Skip to content

Instantly share code, notes, and snippets.

@manchumahara
Created March 23, 2014 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manchumahara/9718860 to your computer and use it in GitHub Desktop.
Save manchumahara/9718860 to your computer and use it in GitHub Desktop.
Wordpress default archive widget doesn't have any option for showing author archive widget. This small code snippet will convert default archive widget into a author archive widget. For showing the link I hooked only for month link, same filter can be applied to other archive link for day or year archive link for any author. This works on author…
add_filter('getarchives_where', 'getarchives_where_author');
add_filter('month_link', 'month_link_author');
/**
* Custom function to filter query for author archive widget, this works on author page only
*
* @param $where
* @return string
*/
function getarchives_where_author($where){
global $wpdb, $wp_locale;
if(is_author()){
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
//var_dump($curauth);
$where .= ' AND post_author = '.$curauth->ID.' ';
}
return $where;
}
/**
* Custom function to filter author archive link for month
*
* @param $link
* @return string
*/
function month_link_author($link){
global $wpdb, $wp_locale;
if(is_author()){
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
//author_name=asaduzzeman
if(strpos($current_url, '?') !== false) {
$link .= '&author_name='.$curauth->user_login;
}
else{
$link .= '?author_name='.$curauth->user_login;
}
}
return $link;
}
@mrpritchett
Copy link

This throws errors b/c $current_url is not set. But even if you set it, it doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment