Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fixes the Categories Widget count
function ix_change_term_counts( $terms, $taxonomies ) {
global $wp_query;
if ( is_admin() || is_ajax() ) {
return $terms;
}
if ( ! isset( $taxonomies[0] ) ) {
return $terms;
}
$user_id = get_current_user_id();
foreach ( $terms as &$term ) {
if ( is_object( $term ) ) {
$post_ids = get_posts(array(
'numberposts' => -1, // get all posts.
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term->term_id,
),
),
'fields' => 'ids', // Only get post IDs
));
$cnt = 0;
if ( sizeof( $post_ids ) > 0 ) {
foreach ( $post_ids as $post_id ) {
if ( Groups_Post_Access::user_can_read_post( $post_id, $user_id ) ) {
$cnt++;
}
}
}
$term->count = $cnt;
}
}
return $terms;
}
add_filter( 'get_terms', 'ix_change_term_counts', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.