Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codearachnid/9655690 to your computer and use it in GitHub Desktop.
Save codearachnid/9655690 to your computer and use it in GitHub Desktop.
Filter widgets in sidebar to filter taxonomy terms by author when on the author template - the request: http://wordpress.stackexchange.com/questions/138097/author-template-filter-sidebar-widgets-by-author
<?php
/*
* For updates to this code please use the latest here:
*
* @link https://github.com/codearachnid/WPSE/blob/master/filter-by-author-widget-taxonomy-terms.php
*/
add_filter( 'widget_tag_cloud_args', 'filter_categories_by_author' );
add_filter( 'widget_categories_dropdown_args', 'filter_categories_by_author' );
add_filter( 'widget_categories_args', 'filter_categories_by_author' );
function filter_categories_by_author( $args ){
// only process if on the author template
if( is_author() ){
// get the author ID from the template
$author_id = get_the_author_meta( 'ID' );
// define taxonomy by the supplied widget or assumed default
$taxonomy = !empty( $args['taxonomy'] ) ? $args['taxonomy'] : 'category';
// filter by including only IDs associated to the author
$args['include'] = get_taxonomy_ids_by_author( $author_id, $taxonomy );
}
return $args;
}
function get_taxonomy_ids_by_author( $user_id, $taxonomy = 'category' ){
global $wpdb;
return $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT(terms.term_id) as ID
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE 1=1 AND (
posts.post_status = 'publish' AND
posts.post_author = %d AND
tax.taxonomy = '%s' )
ORDER BY terms.name ASC
", $user_id, $taxonomy ) );
}
@codearachnid
Copy link
Author

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