Hide specific author's posts from categories list

15 Views Asked by At

It's a wordpress website. I have a user, let's say "admin2" and some categories on the website. Each category has a number of posts. I want to exclude the posts of user "admin2" from the item count in each category. Thank you!

function exclude_user_posts_from_category_count( $counts, $type, $perm ) {
if ( is_admin() ) {
$user_posts = get_posts( array(
'author_name' => 'admin2',
'fields' => 'ids',
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => -1,));
foreach ( $counts as $key => $value ) {
$args = array(
'category' => $key,
'post__not_in' => $user_posts,);
$counts[ $key ] = wp_count_posts( $type, 'readable', $args );
}
}
return $counts;
}
add_filter( 'wp_count_posts', 'exclude_user_posts_from_category_count', 10, 3 );
0

There are 0 best solutions below