Using WordPress ‘is_sticky()’ PHP function

The is_sticky() WordPress PHP function determines whether a post is sticky.

Usage

is_sticky($post_id);

Example:

if (is_sticky()) {
    echo 'This is a sticky post!';
}

Parameters

  • $post_id (int, optional) – Post ID. Default is the ID of the global $post.

More information

See WordPress Developer Resources: is_sticky()

Examples

Check if a post is sticky

This code checks if a specific post with ID 42 is sticky:

if (is_sticky(42)) {
    echo 'Post 42 is sticky!';
}

Display a message on all sticky posts

This code displays a message on all sticky posts in The Loop:

if (have_posts()) {
    while (have_posts()) {
        the_post();

        if (is_sticky()) {
            echo 'This post is sticky!';
        }

        the_title();
        the_content();
    }
}

Add a CSS class to sticky posts

This code adds a CSS class sticky-post to all sticky posts:

$post_classes = is_sticky() ? 'sticky-post' : '';

echo '<div class="' . $post_classes . '">';
the_title();
the_content();
echo '</div>';

Show only sticky posts in a custom query

This code creates a custom query to display only sticky posts:

$sticky_posts = get_option('sticky_posts');

$query_args = array(
    'post__in' => $sticky_posts
);

$sticky_query = new WP_Query($query_args);

if ($sticky_query->have_posts()) {
    while ($sticky_query->have_posts()) {
        $sticky_query->the_post();
        the_title();
        the_content();
    }
}

Display a special layout for sticky posts

This code displays a different layout for sticky posts in The Loop:

if (have_posts()) {
    while (have_posts()) {
        the_post();

        if (is_sticky()) {
            get_template_part('content', 'sticky');
        } else {
            get_template_part('content');
        }
    }
}