Skip to content

Instantly share code, notes, and snippets.

@davoraltman
Created November 16, 2018 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davoraltman/35a41f28e39fe3aee86388af219401ec to your computer and use it in GitHub Desktop.
Save davoraltman/35a41f28e39fe3aee86388af219401ec to your computer and use it in GitHub Desktop.
Remove Uncategorized posts from Jetpacks Top Posts & Pages Widget
function daki_remove_post_top_posts( $posts, $post_ids, $count ) {
foreach ( $posts as $k => $post ) {
// Get the list of categories for that post.
$categories = wp_get_post_categories(
$post['post_id'],
// We only need the category slug here.
array( 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'slugs' )
);
// Does one of those categories use a category with the "uncategorized" slug?
if ( is_array( $categories ) && ! empty ( $categories ) ) {
foreach ( $categories as $category ) {
if ( 'uncategorized' == $category ) {
unset( $posts[$k] );
}
}
}
}
return $posts;
}
add_filter( 'jetpack_widget_get_top_posts', 'daki_remove_post_top_posts', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment