Sticky posts in WordPress will stick to the top of the post lists. Moderators can set a post to be sticky inside the WordPress admin panel. Developers can check if a post is sticky or not in WordPress, using the is_sticky function. In this WordPress tutorial, We're going to set a sticky post and check if the is_sticky function detects it as a sticky post or not.
How to check if a specific post is sticky when developing WordPress?
is_sticky
function will help you in this situation.
Just give the is_sticky
function a post ID , and it will determine if a post is sticky or not with a true/false value.
The current queried post will be targeted if you do not specify a post ID.
is_sticky function syntax
The syntax is pretty easy and understandable:
is_sticky(int $postId)
is_sticky function example
To check if the post with ID 1 is sticky or not in WordPress, you can use this code:
$postId = 1;
if(is_sticky(int $postId)){
//post is sticky!
}
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Plugin Development WordPress Theme Development
What do you think about "How to Check if a Post is Sticky in WordPress (+Example)"?