Filter the Top Posts and Pages.
Parameters
- $posts
array Array of the most popular posts.
- $post_ids
array Array of Post IDs.
- $count
string Number of Top Posts we want to display.
Changelog
- Introduced in Jetpack 3.0.0
How to use this hook
Notes
Example 1: You can use this filter to remove a post from the Top Posts & Pages Widget. Here is how you would use it:
function jeherve_remove_post_top_posts( $posts, $post_ids, $count ) {
foreach ( $posts as $k => $post ) {
// Replace 1215 by the ID of the Post you want to remove
if ( '1215' == $post['post_id'] ) {
unset( $posts[$k] );
}
}
return $posts;
}
add_filter( 'jetpack_widget_get_top_posts', 'jeherve_remove_post_top_posts', 10, 3 );
Example 2: Remove the current post from the Top Posts & Pages Widget.
function htdat_remove_current_post_top_posts( $posts, $post_ids, $count ) {
if ( ! is_singular() ) {
return $posts;
}
global $post;
$current_post_id = $post->ID;
foreach ( $posts as $k => $post ) {
if ( $current_post_id == $post['post_id'] ) {
unset( $posts[$k] );
}
}
return $posts;
}
add_filter( 'jetpack_widget_get_top_posts', 'htdat_remove_current_post_top_posts', 10, 3 );