apply_filters ( 'jetpack_widget_get_top_posts', array $posts, array $post_ids, string $count )

Filter the Top Posts and Pages.

Source file: modules/widgets/top-posts.php

View in GitHub

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

Since: Jetpack 3.0.0


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 );

Tutorials

Have a note to contribute?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s