Since WordPress 2.7, we can use sticky posts which keep posts at the top when going through the loop of posts. Even though the sticky posts were added way back in 2008, this feature still only works with built-in posts. With custom post types, it is currently not yet available. In this article, we will learn what sticky posts are, why they weren't added yet for custom post types, and how to make them stick anyway.
What is a sticky feature in WordPress?
When we loop through posts on a page, for example when showing posts on a blog, we sometimes want to feature specific post and want those posts to be listed first regardless of when it was published. This is where the sticky feature can become useful. They will be kept at the top until newer sticky posts are added. We enable this option in the post editor.
When using Block Editor, the option is named "Stick to the top of the blog" shown like this:
And when using Classic Editor, the option is named a bit differently "Stick this post to the front page" as shown below:
The posts with this option checked will stay at the top when going through the post loop. But why is this sticky option not available for custom post types?
Sticky feature missing for custom post types
There is a very old WordPress Feature request regarding this issue, but the sticky option still hasn't been implemented for custom post types. The reason seems to be backward compatibility.
A theme or a widget might use the code below to get the array containing IDs of all the sticky posts:
get_option('sticky_posts');
If we also have custom post types with a sticky option, IDs of those sticky custom post types will be returned as well, which might cause problems with the theme or widget using the above code.
One solution would be to loop through all the IDs in the returned array of get_option('sticky_posts')
and check if the post ID is a regular 'post' type or not:
$my_sticky_posts=get_option('sticky_posts');
foreach($my_sticky_posts as $stickiy_id) {
if ( get_post_type( $sticky_id ) == 'post' ) {
//only regular sticky posts here
}
}
The get_post_type() returns the type of the post. For regular built-in posts, it will return 'post'. This way, all the sticky custom post types will be filtered out.
Creating sticky custom post types with the help of a plugin
So now we know what the sticky feature is in WordPress and also how to fix a theme for backward compatibility, but how do we also make a custom post to have the sticky option enabled?
It's quite easy as there is a WordPress plugin Custom Post Type Sticky just for this purpose.
The plugin is very simple. It has no settings, we just need to enable it and it will add a "Stick this post to the front page" checkbox in the custom post type editor:
Conclusion
For years now, the sticky feature in WordPress is available for regular posts, but it is not yet available for the custom post type. The reason seems to be backward compatibility since some themes will assume the sticky option is used only for regular posts. To make the custom post types sticky regardless, we can use the WordPress plugin Custom Post Type Sticky plugin that adds a "Stick this post to the front page" checkbox in the custom post type editor.
Angelika
January 5, 2022Once upon a time I had a problem with it, which took me a long time, here I found the answer.
"For years now, the sticky feature in WordPress is available for regular posts, but it is not yet available for the custom post type."
Thanks, all the best for the new year.