Using WordPress ‘is_sticky’ PHP filter

The is_sticky WordPress PHP Filter checks if a post is sticky or not.

Usage

add_filter('is_sticky', 'your_custom_function', 10, 2);

function your_custom_function($is_sticky, $post_id) {
    // your custom code here

    return $is_sticky;
}

Parameters

  • $is_sticky (bool) – Whether a post is sticky.
  • $post_id (int) – The post ID.

More information

See WordPress Developer Resources: is_sticky

Examples

Mark all posts by a specific author as sticky

This code will make all posts by the author with ID 5 sticky.

add_filter('is_sticky', 'mark_author_posts_sticky', 10, 2);

function mark_author_posts_sticky($is_sticky, $post_id) {
    $post = get_post($post_id);
    if ($post->post_author == 5) {
        return true;
    }
    return $is_sticky;
}

Mark posts in a specific category as sticky

This code will make all posts in the “Featured” category sticky.

add_filter('is_sticky', 'mark_category_posts_sticky', 10, 2);

function mark_category_posts_sticky($is_sticky, $post_id) {
    if (has_category('Featured', $post_id)) {
        return true;
    }
    return $is_sticky;
}

Unstick all sticky posts on a specific date

This code will unstick all sticky posts on April 1st.

add_filter('is_sticky', 'unstick_posts_on_date', 10, 2);

function unstick_posts_on_date($is_sticky, $post_id) {
    if (date('Y-m-d') == '2023-04-01') {
        return false;
    }
    return $is_sticky;
}

Stick only the latest 5 posts

This code will make only the latest 5 posts sticky.

add_filter('is_sticky', 'stick_only_latest_posts', 10, 2);

function stick_only_latest_posts($is_sticky, $post_id) {
    $latest_posts = get_posts(array('numberposts' => 5));
    $post_ids = wp_list_pluck($latest_posts, 'ID');

    if (in_array($post_id, $post_ids)) {
        return true;
    }
    return $is_sticky;
}

Stick a post only for logged-in users

This code will make a post with ID 123 sticky only for logged-in users.

add_filter('is_sticky', 'stick_post_for_logged_in_users', 10, 2);

function stick_post_for_logged_in_users($is_sticky, $post_id) {
    if ($post_id == 123 && is_user_logged_in()) {
        return true;
    }
    return $is_sticky;
}