Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Add Sticky Posts in WordPress Custom Post Type Archives

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Add Sticky Posts in WordPress Custom Post Type Archives

Recently one of our users asked us if it was possible to add sticky posts to custom post type archives. By default, WordPress has the sticky functionality available for posts, but not for other post types. In this article we will show you how to add sticky posts in WordPress custom post type archives. Before we move forward, you would probably want to learn how to create custom post types in WordPress.

Adding Sticky Posts in Custom Post Types

First thing you need to do is install and activate the Sticky Custom Post Types plugin. After activating the plugin, go to Settings » Reading and scroll down to the section Sticky Custom Post Types. Next, you need to choose the custom post types where you want Stick This option to be enabled.

Enabling sticky posts for custom post types

Now what we have done here is that we have added sticky posts feature to our custom post types. Sticky posts in custom post types will be displayed on the front page just like regular sticky posts.

The problem is that by default WordPress only shows sticky posts on the home page. It does not show sticky posts on archive pages.

Displaying Sticky Posts in Custom Post Type Archives

Lets assume that you have a custom post type for Movie Reviews with sticky posts enabled using the plugin we have mentioned above. Now you want your sticky posts in movie reviews post types to be displayed differently and on top of non-sticky regular movie reviews. Like this:

Showing a sticky post on a custom post type archive page

To achieve this goal, first thing you need is an archive template for your custom post type like this: archive-post-type.php. Learn how to create custom post type archive page. For example, if you have a custom post type movie-reviews then your archive page template should be archive-movie-reviews.php. If you do not have a template, create one. Simply copy the contents of archive.php in your theme’s directory and paste them into a new file archive-your-post-type.php.

The next step is to add this code in your theme’s functions.php file:

function wpb_cpt_sticky_at_top( $posts ) {
    // apply it on the archives only
    if ( is_main_query() && is_post_type_archive() ) {
        global $wp_query;
        $sticky_posts = get_option( 'sticky_posts' );
        $num_posts = count( $posts );
        $sticky_offset = 0;
        // Find the sticky posts
        for ($i = 0; $i < $num_posts; $i++) {
            // Put sticky posts at the top of the posts array
            if ( in_array( $posts[$i]->ID, $sticky_posts ) ) {
                $sticky_post = $posts[$i];
                // Remove sticky from current position
                array_splice( $posts, $i, 1 );
                // Move to front, after other stickies
                array_splice( $posts, $sticky_offset, 0, array($sticky_post) );
                // Remove post from sticky posts array
                $offset = array_search($sticky_post->ID, $sticky_posts);
                unset( $sticky_posts[$offset] );
        // Look for more sticky posts if needed
        if ( !empty( $sticky_posts) ) {
            $stickies = get_posts( array(
                'post__in' => $sticky_posts,
                'post_type' => $wp_query->query_vars['post_type'],
                'post_status' => 'publish',
                'nopaging' => true
            ) );
            foreach ( $stickies as $sticky_post ) {
                array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
    return $posts;
add_filter( 'the_posts', 'wpb_cpt_sticky_at_top' );

// Add sticky class in article title to style sticky posts differently

function cpt_sticky_class($classes) {
			if ( is_sticky() ) : 
			$classes[] = 'sticky';
	        return $classes;
		return $classes;
	add_filter('post_class', 'cpt_sticky_class');

The above code would move your sticky posts to the top, and if your theme is using post_class() function, then it would add sticky in the post class.

You can style your sticky posts by using .sticky class in your stylesheet. Example:

.sticky { 
background-position:right top;

Styling sticky posts

We hope this article helped you add sticky posts in custom post type archives. For questions and feedback please leave a comment below.

Source: Tareq Hasan

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Pat Ducat says:

    This works good however it makes it sticky on every page of a paginated archive. Is this how the built-in sticky functionality for standard posts work too?

  2. Aaron says:

    How could i set this up to work with a custom taxonomy archive page?
    I’ve tried adding ‘is_tax’ and ‘is_category’ instead of the is_post_type_archive() on line 4 of your function but it just breaks the page.

    I’m missing something obviously but can’t seem to find it.
    Any ideas?

  3. Daniel Dropik says:

    Thanks. Is it possible to adapt this tutorial to display sticky posts onto a specialized page template, rather than on the archives page? If so how might I accomplish this?

    • WPBeginner Support says:

      Daniel, yes sure it can be done in a separate page template. Simply create a custom template and follow the instructions given above.

  4. Shawn says:

    How do you do this for Custom Taxonomy.php instead of archives.

    • WPBeginner Support says:

      if the custom taxonomy is displaying post types with sticky posts support, then you can display it in the same manner. Instead of archive-post-type.php template, make changes in taxonomy-custom-taxonomy.php template.

  5. Mr.Ultra says:

    Thanks. This is useful.
    But if it’s possible not using a plugin to add sticky functionality to custom post types?
    Can you share the snippet?

  6. Anir says:

    Very informative, thanks for sharing. It helps a lot.

  7. Daniel says:

    Really nice tut.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.