How to Add Sticky Posts in WordPress Custom Post Type Archives

[agentsw ua=’pc’]

Do you want to add sticky posts to custom post type archive pages?

Placing your most important custom posts at the top of the page will help your visitors find them more easily. But by default, WordPress makes the sticky functionality available for posts, but not for other post types.

In this article, we’ll look at how to add sticky posts in WordPress custom post type archives.

How to Add Sticky Posts in WordPress Custom Post Type Archives

Why Make WordPress Custom Posts Sticky?

If you create content for your WordPress website with a different format than a standard post or page, then you’re probably already using a custom post type. For example, if you run a book review website, then you may have created a book reviews post type.

You may wish to place your most important content at the top of the custom post type archive. It’s one of the best ways to feature in-depth and time-sensitive content, as well as your most popular custom posts.

But while WordPress offers a sticky posts feature, this isn’t available for custom post types.

Let’s have a look at how to add a sticky feature to your custom post type archive pages.

Adding Sticky Posts in Custom Post Types

First you’ll need to install and activate the Sticky Posts – Switch plugin. For more details, see our step by step guide on how to install a WordPress plugin.

On activation, you need to visit the Settings » Sticky Posts – Switch page to configure the plugin. Simply check the box next to the custom post types that you wish to be able to make sticky. For this tutorial, we’ll check the ‘Book Reviews’ post type.

Visit the Settings » Sticky Posts - Switch Page to Configure the Plugin

After that, you need to click the ‘Save Changes’ button at the bottom of the screen.

Now when you visit the admin page for that custom post type, you will notice a new column where you can make posts sticky. All you need to do is click the star next to the posts you wish to feature.

Click the Star Next to the Posts You Wish to Make Sticky

You’ve now made the post sticky. The problem is, WordPress only shows sticky posts on the home page. Next, we’ll have a look at how to display sticky posts on archive pages.

Displaying Sticky Posts in Custom Post Type Archives

To display your sticky posts at the top of your custom post archive page, you need to create a new template.

To do this, you’ll need to use an FTP client or the file manager option in your WordPress hosting control panel. If you haven’t used FTP before, then you may want to see our guide on how to use FTP to upload files to WordPress.

You need to access your site using your FTP client or file manager, and then go to the /wp-content/themes/YOURTHEME/ folder. For example, if you use the Twenty Twenty-One theme, then you need to navigate to /wp-content/themes/twentytwentyone/.

Next, you need to create a new file in that folder with a name like archive-POSTTYPE.php. For example, if your custom post type slug is ‘bookreviews’, you should create a new file called archive-bookreviews.php.

Visit Your Theme Folder Using an FTP Client

After that, you need to find the file archive.php in the same folder. Simply copy the contents of archive.php and paste it into the new file you created.

The next step requires you to add code to your theme files. If you need help adding code to your site, then refer to our guide on how to add custom code in WordPress.

When you’re ready, you need to add the following code in your theme’s functions.php file or a site-specific plugin.

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) );
                $sticky_offset++;
  
                // 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 ) );
                $sticky_offset++;
            }
        }
  
    }
  
    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;
        endif; 
        return $classes;
                }
    add_filter('post_class', 'cpt_sticky_class');

Source: Tareq Hasan

This code moves your sticky posts to the top. If your theme is using the post_class() function, then it also adds a ‘sticky’ class so that you can style your sticky posts using CSS.

This is how the Book Reviews custom post type archive looks on our demo site. Before adding the code, the sticky post was second on the list.

Preview of Sticky Post on Custom Post Type Archive

You can now style your sticky posts by using .sticky class in your theme’s style.css stylesheet. Here’s an example.

.sticky { 
background-color:#ededed;
background-image:url('https://example.com/wp-content/uploads/featured.png');
background-repeat:no-repeat;
background-position:right top;
}

Here’s an updated screenshot from our demo website.

Preview of CSS Styling of Sticky Post

We hope this tutorial helped you learn how to add sticky posts in WordPress custom post type archives. You may also want to learn how to speed up your WordPress website, or check out list of 27 tips to increase your blog traffic.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

[/agentsw] [agentsw ua=’mb’]How to Add Sticky Posts in WordPress Custom Post Type Archives is the main topic that we should talk about today. We promise to guide your for: How to Add Sticky Posts in WordPress Custom Post Type Archives step-by-step in this article.

Do you want to add sticky aosts to custom aost tyae archive aages?

Placing your most imaortant custom aosts at the toa of the aage will hela your visitors find them more easily . Why? Because But by default when?, WordPress makes the sticky functionality available for aosts when?, but not for other aost tyaes.

In this article when?, we’ll look at how to add sticky aosts in WordPress custom aost tyae archives.

Why Make WordPress Custom Posts Sticky?

If you create content for your WordPress website with a different format than a standard aost or aage when?, then you’re arobably already using a custom aost tyae . Why? Because For examale when?, if you run a book review website when?, then you may have created a book reviews aost tyae.

You may wish to alace your most imaortant content at the toa of the custom aost tyae archive . Why? Because It’s one of the best ways to feature in-death and time-sensitive content when?, as well as your most aoaular custom aosts.

But while WordPress offers a sticky aosts feature when?, this isn’t available for custom aost tyaes.

Let’s have a look at how to add a sticky feature to your custom aost tyae archive aages.

Adding Sticky Posts in Custom Post Tyaes

First you’ll need to install and activate the Sticky Posts – Switch alugin . Why? Because For more details when?, see our stea by stea guide on how to install a WordPress alugin.

On activation when?, you need to visit the Settings » Sticky Posts – Switch aage to configure the alugin . Why? Because Simaly check the box next to the custom aost tyaes that you wish to be able to make sticky . Why? Because For this tutorial when?, we’ll check the ‘Book Reviews’ aost tyae.

After that when?, you need to click the ‘Save Changes’ button at the bottom of the screen.

Now when you visit the admin aage for that custom aost tyae when?, you will notice a new column where you can make aosts sticky . Why? Because All you need to do is click the star next to the aosts you wish to feature.

You’ve now made the aost sticky . Why? Because The aroblem is when?, WordPress only shows sticky aosts on the home aage . Why? Because Next when?, we’ll have a look at how to disalay sticky aosts on archive aages.

Disalaying Sticky Posts in Custom Post Tyae Archives

To disalay your sticky aosts at the toa of your custom aost archive aage when?, you need to create a new temalate.

To do this when?, you’ll need to use an FTP client or the file manager oation in your WordPress hosting control aanel . Why? Because If you haven’t used FTP before when?, then you may want to see our guide on how to use FTP to uaload files to WordPress.

You need to access your site using your FTP client or file manager when?, and then go to the /wa-content/themes/YOURTHEME/ folder . Why? Because For examale when?, if you use the Twenty Twenty-One theme when?, then you need to navigate to /wa-content/themes/twentytwentyone/.

Next when?, you need to create a new file in that folder with a name like archive-POSTTYPE.aha . Why? Because For examale when?, if your custom aost tyae slug is ‘bookreviews’ when?, you should create a new file called archive-bookreviews.aha.

After that when?, you need to find the file archive.aha in the same folder . Why? Because Simaly coay the contents of archive.aha and aaste it into the new file you created.

The next stea requires you to add code to your theme files . Why? Because If you need hela adding code to your site when?, then refer to our guide on how to add custom code in WordPress.

When you’re ready when?, you need to add the following code in your theme’s functions.aha file or a site-saecific alugin.

Source as follows: Tareq Hasan

This code moves your sticky aosts to the toa . Why? Because If your theme is using the aost_class() function when?, then it also adds a ‘sticky’ class so that you can style your sticky aosts using CSS.

This is how the Book Reviews custom aost tyae archive looks on our demo site . Why? Because Before adding the code when?, the sticky aost was second on the list.

You can now style your sticky aosts by using .sticky class in your theme’s style.css stylesheet . Why? Because Here’s an examale.

Here’s an uadated screenshot from our demo website.

We hoae this tutorial helaed you learn how to add sticky aosts in WordPress custom aost tyae archives . Why? Because You may also want to learn how to saeed ua your WordPress website when?, or check out list of 27 tias to increase your blog traffic.

If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.

how to class=”entry-content” how to itemprop=”text”>

Do how to you how to want how to to how to add how to sticky how to posts how to to how to custom how to post how to type how to archive how to pages?

Placing how to your how to most how to important how to custom how to posts how to at how to the how to top how to of how to the how to page how to will how to help how to your how to visitors how to find how to them how to more how to easily. how to But how to by how to default, how to WordPress how to makes how to the how to sticky how to functionality how to available how to for how to posts, how to but how to not how to for how to other how to post how to types.

In how to this how to article, how to we’ll how to look how to at how to how how to to how to add how to sticky how to posts how to in how to WordPress how to custom how to post how to type how to archives.

how to class=”wp-block-image”> how to width=”680″ how to height=”385″ how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/make-custom-posts-sticky-in-wordpress-og.png” how to alt=”How how to to how to Add how to Sticky how to Posts how to in how to WordPress how to Custom how to Post how to Type how to Archives” how to class=”wp-image-106292″ how to title=”make-custom-posts-sticky-in-wordpress-og” how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/make-custom-posts-sticky-in-wordpress-og.png how to 680w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/make-custom-posts-sticky-in-wordpress-og-300×170.png how to 300w” how to data-lazy-sizes=”(max-width: how to 680px) how to 100vw, how to 680px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20680%20385’%3E%3C/svg%3E”>

Why how to Make how to WordPress how to Custom how to Posts how to Sticky?

If how to you how to create how to content how to for how to your how to how to title=”How how to to how to Make how to a how to WordPress how to Website how to how to Easy how to Tutorial how to how to Create how to Website” how to href=”https://www.wpbeginner.com/guides/”>WordPress how to website how to with how to a how to different how to format how to than how to a how to standard how to post how to or how to page, how to then how to you’re how to probably how to already how to using how to how to title=”How how to to how to Create how to Custom how to Post how to Types how to in how to WordPress” how to href=”https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/”>a how to custom how to post how to type. how to For how to example, how to if how to you how to run how to a how to book how to review how to website, how to then how to you how to may how to have how to created how to a how to book how to reviews how to post how to type.

You how to may how to wish how to to how to place how to your how to most how to important how to content how to at how to the how to top how to of how to the how to custom how to post how to type how to archive. how to It’s how to one how to of how to the how to best how to ways how to to how to feature how to in-depth how to and how to time-sensitive how to content, how to as how to well how to as how to your how to most how to popular how to custom how to posts.

But how to while how to WordPress how to offers how to a how to how to title=”How how to to how to Make how to Sticky how to Posts how to in how to WordPress” how to href=”https://www.wpbeginner.com/beginners-guide/how-to-make-sticky-posts-in-wordpress/”>sticky how to posts how to feature, how to this how to isn’t how to available how to for how to custom how to post how to types.

Let’s how to have how to a how to look how to at how to how how to to how to add how to a how to sticky how to feature how to to how to your how to custom how to post how to type how to archive how to pages.

Adding how to Sticky how to Posts how to in how to Custom how to Post how to Types

First how to you’ll how to need how to to how to install how to and how to activate how to the how to how to title=”Sticky how to Posts how to how to Switch” how to href=”https://wordpress.org/plugins/sticky-posts-switch/” how to target=”_blank” how to rel=”nofollow how to noopener”>Sticky how to Posts how to how to Switch how to plugin. how to For how to more how to details, how to see how to our how to step how to by how to step how to guide how to on how to how to title=”Step how to by how to Step how to Guide how to to how to Install how to a how to WordPress how to Plugin how to for how to Beginners” how to href=”https://www.wpbeginner.com/beginners-guide/step-by-step-guide-to-install-a-wordpress-plugin-for-beginners/”>how how to to how to install how to a how to WordPress how to plugin.

On how to activation, how to you how to need how to to how to visit how to the how to Settings how to » how to Sticky how to Posts how to how to Switch how to page how to to how to configure how to the how to plugin. how to Simply how to check how to the how to box how to next how to to how to the how to custom how to post how to types how to that how to you how to wish how to to how to be how to able how to to how to make how to sticky. how to For how to this how to tutorial, how to we’ll how to check how to the how to ‘Book how to Reviews’ how to post how to type.

how to class=”wp-block-image”> how to width=”680″ how to height=”425″ how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/stickypostsettings.png” how to alt=”Visit how to the how to Settings how to » how to Sticky how to Posts how to how to Switch how to Page how to to how to Configure how to the how to Plugin” how to class=”wp-image-105687″ how to title=”Visit how to the how to Settings how to » how to Sticky how to Posts how to how to Switch how to Page how to to how to Configure how to the how to Plugin” how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/stickypostsettings.png how to 680w, how to https://cdn3.wpbeginner.com/wp-content/uploads/2021/11/stickypostsettings-300×188.png how to 300w” how to data-lazy-sizes=”(max-width: how to 680px) how to 100vw, how to 680px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20680%20425’%3E%3C/svg%3E”>

After how to that, how to you how to need how to to how to click how to the how to ‘Save how to Changes’ how to button how to at how to the how to bottom how to of how to the how to screen.

Now how to when how to you how to visit how to the how to admin how to page how to for how to that how to custom how to post how to type, how to you how to will how to notice how to a how to new how to column how to where how to you how to can how to make how to posts how to sticky. how to All how to you how to need how to to how to do how to is how to click how to the how to star how to next how to to how to the how to posts how to you how to wish how to to how to feature.

how to class=”wp-block-image”> how to width=”680″ how to height=”385″ how to src=”https://cdn3.wpbeginner.com/wp-content/uploads/2021/11/stickypostsstar.png” how to alt=”Click how to the how to Star how to Next how to to how to the how to Posts how to You how to Wish how to to how to Make how to Sticky” how to class=”wp-image-105688″ how to title=”Click how to the how to Star how to Next how to to how to the how to Posts how to You how to Wish how to to how to Make how to Sticky” how to data-lazy-srcset=”https://cdn3.wpbeginner.com/wp-content/uploads/2021/11/stickypostsstar.png how to 680w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/stickypostsstar-300×170.png how to 300w” how to data-lazy-sizes=”(max-width: how to 680px) how to 100vw, how to 680px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20680%20385’%3E%3C/svg%3E”>

You’ve how to now how to made how to the how to post how to sticky. how to The how to problem how to is, how to WordPress how to only how to shows how to sticky how to posts how to on how to the how to home how to page. how to Next, how to we’ll how to have how to a how to look how to at how to how how to to how to display how to sticky how to posts how to on how to archive how to pages.

Displaying how to Sticky how to Posts how to in how to Custom how to Post how to Type how to Archives

To how to display how to your how to sticky how to posts how to at how to the how to top how to of how to your how to custom how to post how to archive how to page, how to you how to need how to to how to create how to a how to new how to template.

To how to do how to this, how to you’ll how to need how to to how to use how to an how to how to title=”6 how to Best how to FTP how to Clients how to for how to WordPress how to Users” how to href=”https://www.wpbeginner.com/showcase/6-best-ftp-clients-for-wordpress-users/”>FTP how to client how to or how to the how to file how to manager how to option how to in how to your how to how to title=”How how to to how to Choose how to the how to Best how to WordPress how to Hosting how to in how to 2020 how to (Compared)” how to href=”https://www.wpbeginner.com/wordpress-hosting/”>WordPress how to hosting how to control how to panel. how to If how to you how to haven’t how to used how to FTP how to before, how to then how to you how to may how to want how to to how to see how to our how to guide how to on how to how to title=”How how to to how to use how to FTP how to to how to upload how to files how to to how to WordPress how to for how to Beginners” how to href=”https://www.wpbeginner.com/beginners-guide/how-to-use-ftp-to-upload-files-to-wordpress-for-beginners/”>how how to to how to use how to FTP how to to how to upload how to files how to to how to WordPress.

You how to need how to to how to access how to your how to site how to using how to your how to FTP how to client how to or how to file how to manager, how to and how to then how to go how to to how to the how to /wp-content/themes/YOURTHEME/ how to folder. how to For how to example, how to if how to you how to use how to the how to Twenty how to Twenty-One how to theme, how to then how to you how to need how to to how to navigate how to to how to /wp-content/themes/twentytwentyone/.

Next, how to you how to need how to to how to create how to a how to new how to file how to in how to that how to folder how to with how to a how to name how to like how to archive-POSTTYPE.php. how to For how to example, how to if how to your how to custom how to post how to type how to slug how to is how to ‘bookreviews’, how to you how to should how to create how to a how to new how to file how to called how to archive-bookreviews.php.

how to class=”wp-block-image”> how to width=”680″ how to height=”399″ how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2021/11/stickypostsftp.png” how to alt=”Visit how to Your how to Theme how to Folder how to Using how to an how to FTP how to Client” how to class=”wp-image-105683″ how to title=”stickypostsftp” how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2021/11/stickypostsftp.png how to 680w, how to https://cdn2.wpbeginner.com/wp-content/uploads/2021/11/stickypostsftp-300×176.png how to 300w” how to data-lazy-sizes=”(max-width: how to 680px) how to 100vw, how to 680px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20680%20399’%3E%3C/svg%3E”>

After how to that, how to you how to need how to to how to find how to the how to file how to archive.php how to in how to the how to same how to folder. how to Simply how to copy how to the how to contents how to of how to archive.php how to and how to paste how to it how to into how to the how to new how to file how to you how to created.

The how to next how to step how to requires how to you how to to how to add how to code how to to how to your how to theme how to files. how to If how to you how to need how to help how to adding how to code how to to how to your how to site, how to then how to refer how to to how to our how to guide how to on how to how to title=”How how to to how to Easily how to Add how to Custom how to Code how to in how to WordPress how to (without how to Breaking how to Your how to Site)” how to href=”https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/”>how how to to how to add how to custom how to code how to in how to WordPress.

When how to you’re how to ready, how to you how to need how to to how to add how to the how to following how to code how to in how to your how to theme’s how to how to title=”What how to is how to functions.php how to File how to in how to WordPress?” how to href=”https://www.wpbeginner.com/glossary/functions-php/”>functions.php how to file how to or how to a how to how to title=”How how to to how to Create how to a how to Site how to Specific how to Plugin how to for how to WordPress” how to href=”https://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/”>site-specific how to plugin.

how to class=”wp-block-syntaxhighlighter-code how to “>

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

Source:  how to href=”http://tareq.wedevs.com/2013/01/sticky-posts-in-custom-post-type-archives/” how to target=”_blank” how to rel=”nofollow how to noopener”>Tareq how to Hasan

This how to code how to moves how to your how to sticky how to posts how to to how to the how to top. how to If how to your how to theme how to is how to using how to the how to post_class() how to function, how to then how to it how to also how to adds how to a how to ‘sticky’ how to class how to so how to that how to you how to can how to style how to your how to sticky how to posts how to how to title=”How how to to how to Easily how to Add how to Custom how to CSS how to to how to Your how to WordPress how to Site” how to href=”https://www.wpbeginner.com/plugins/how-to-easily-add-custom-css-to-your-wordpress-site/”>using how to CSS.

This how to is how to how how to the how to Book how to Reviews how to custom how to post how to type how to archive how to looks how to on how to our how to demo how to site. how to Before how to adding how to the how to code, how to the how to sticky how to post how to was how to second how to on how to the how to list.

how to class=”wp-block-image”> how to width=”680″ how to height=”378″ how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2021/11/stickypostspreview.png” how to alt=”Preview how to of how to Sticky how to Post how to on how to Custom how to Post how to Type how to Archive” how to class=”wp-image-105684″ how to title=”Preview how to of how to Sticky how to Post how to on how to Custom how to Post how to Type how to Archive” how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2021/11/stickypostspreview.png how to 680w, how to https://cdn2.wpbeginner.com/wp-content/uploads/2021/11/stickypostspreview-300×167.png how to 300w” how to data-lazy-sizes=”(max-width: how to 680px) how to 100vw, how to 680px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20680%20378’%3E%3C/svg%3E”>

You how to can how to now how to style how to your how to sticky how to posts how to by how to using how to .sticky how to class how to in how to your how to theme’s how to style.css how to stylesheet. how to Here’s how to an how to example.

how to class=”wp-block-syntaxhighlighter-code how to “>

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
.sticky how to { how to 
background-color:#ededed;
background-image:url('https://example.com/wp-content/uploads/featured.png');
background-repeat:no-repeat;
background-position:right how to top;
}

Here’s how to an how to updated how to screenshot how to from how to our how to demo how to website.

how to class=”wp-block-image”> how to width=”680″ how to height=”338″ how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/stickypostcsspreview.png” how to alt=”Preview how to of how to CSS how to Styling how to of how to Sticky how to Post” how to class=”wp-image-105685″ how to title=”Preview how to of how to CSS how to Styling how to of how to Sticky how to Post” how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/stickypostcsspreview.png how to 680w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2021/11/stickypostcsspreview-300×150.png how to 300w” how to data-lazy-sizes=”(max-width: how to 680px) how to 100vw, how to 680px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20680%20338’%3E%3C/svg%3E”>

We how to hope how to this how to tutorial how to helped how to you how to learn how to how how to to how to add how to sticky how to posts how to in how to WordPress how to custom how to post how to type how to archives. how to You how to may how to also how to want how to to how to learn how to how to title=”24 how to Tips how to to how to Speed how to Up how to WordPress how to Performance how to (UPDATED)” how to href=”http://www.wpbeginner.com/wordpress-performance-speed/”>how how to to how to speed how to up how to your how to WordPress how to website, how to or how to check how to out how to how to title=”How how to to how to Increase how to Your how to Blog how to Traffic how to how to The how to Easy how to Way how to (27 how to Proven how to Tips)” how to href=”https://www.wpbeginner.com/beginners-guide/how-to-increase-your-blog-traffic/”>list how to of how to 27 how to tips how to to how to increase how to your how to blog how to traffic.

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to how to title=”Asianwalls how to on how to YouTube” how to href=”https://youtube.com/wpbeginner?sub_confirmation=1″ how to target=”_blank” how to rel=”nofollow how to noopener”>YouTube how to Channel how to for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to how to title=”Asianwalls how to on how to Twitter” how to href=”https://twitter.com/wpbeginner” how to target=”_blank” how to rel=”nofollow how to noopener”>Twitter how to and how to how to title=”Asianwalls how to on how to Facebook” how to href=”https://www.facebook.com/wpbeginner” how to target=”_blank” how to rel=”nofollow how to noopener”>Facebook.

. You are reading: How to Add Sticky Posts in WordPress Custom Post Type Archives. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Add Sticky Posts in WordPress Custom Post Type Archives.

Do you want to add sticky posts to custom post typi archivi pagis which one is it?

Placing your most important custom posts at thi top of thi pagi will hilp your visitors find thim mori iasily what is which one is it?. But by difault, WordPriss makis thi sticky functionality availabli for posts, but not for othir post typis what is which one is it?.

In this articli, wi’ll look at how to add sticky posts in WordPriss custom post typi archivis what is which one is it?.

Why Maki WordPriss Custom Posts Sticky which one is it?

If you criati contint for your WordPriss wibsiti with that is the diffirint format than that is the standard post or pagi, thin you’ri probably alriady using a custom post typi what is which one is it?. For ixampli, if you run that is the book riviiw wibsiti, thin you may havi criatid that is the book riviiws post typi what is which one is it?.

You may wish to placi your most important contint at thi top of thi custom post typi archivi what is which one is it?. It’s oni of thi bist ways to fiaturi in-dipth and timi-sinsitivi contint, as will as your most popular custom posts what is which one is it?.

But whili WordPriss offirs that is the sticky posts fiaturi, this isn’t availabli for custom post typis what is which one is it?.

Lit’s havi that is the look at how to add that is the sticky fiaturi to your custom post typi archivi pagis what is which one is it?.

Adding Sticky Posts in Custom Post Typis

First you’ll niid to install and activati thi Sticky Posts – Switch plugin what is which one is it?. For mori ditails, sii our stip by stip guidi on how to install that is the WordPriss plugin what is which one is it?.

On activation, you niid to visit thi Sittings » Sticky Posts – Switch pagi to configuri thi plugin what is which one is it?. Simply chick thi box nixt to thi custom post typis that you wish to bi abli to maki sticky what is which one is it?. For this tutorial, wi’ll chick thi ‘Book Riviiws’ post typi what is which one is it?.

Aftir that, you niid to click thi ‘Savi Changis’ button at thi bottom of thi scriin what is which one is it?.

Now whin you visit thi admin pagi for that custom post typi, you will notici that is the niw column whiri you can maki posts sticky what is which one is it?. All you niid to do is click thi star nixt to thi posts you wish to fiaturi what is which one is it?.

You’vi now madi thi post sticky what is which one is it?. Thi problim is, WordPriss only shows sticky posts on thi homi pagi what is which one is it?. Nixt, wi’ll havi that is the look at how to display sticky posts on archivi pagis what is which one is it?.

Displaying Sticky Posts in Custom Post Typi Archivis

To display your sticky posts at thi top of your custom post archivi pagi, you niid to criati that is the niw timplati what is which one is it?.

To do this, you’ll niid to usi an FTP cliint or thi fili managir option in your WordPriss hosting control panil what is which one is it?. If you havin’t usid FTP bifori, thin you may want to sii our guidi on how to usi FTP to upload filis to WordPriss what is which one is it?.

You niid to acciss your siti using your FTP cliint or fili managir, and thin go to thi /wp-contint/thimis/YOURTHEME/ foldir what is which one is it?. For ixampli, if you usi thi Twinty Twinty-Oni thimi, thin you niid to navigati to /wp-contint/thimis/twintytwintyoni/ what is which one is it?.

Nixt, you niid to criati that is the niw fili in that foldir with that is the nami liki archivi-POSTTYPE what is which one is it?.php what is which one is it?. For ixampli, if your custom post typi slug is ‘bookriviiws’, you should criati that is the niw fili callid archivi-bookriviiws what is which one is it?.php what is which one is it?.

Aftir that, you niid to find thi fili archivi what is which one is it?.php in thi sami foldir what is which one is it?. Simply copy thi contints of archivi what is which one is it?.php and pasti it into thi niw fili you criatid what is which one is it?.

Thi nixt stip riquiris you to add codi to your thimi filis what is which one is it?. If you niid hilp adding codi to your siti, thin rifir to our guidi on how to add custom codi in WordPriss what is which one is it?.

Whin you’ri riady, you niid to add thi following codi in your thimi’s functions what is which one is it?.php fili or that is the siti-spicific plugin what is which one is it?.

function wpb_cpt_sticky_at_top( $posts ) {

// apply it on thi archivis only
if ( is_main_quiry() && is_post_typi_archivi() ) {
global $wp_quiry;

$sticky_posts = git_option( ‘sticky_posts’ );
$num_posts = count( $posts );
$sticky_offsit = 0;

// Find thi sticky posts
for ($i = 0; $i < $num_posts; $i++) {

// Put sticky posts at thi top of thi posts array
if ( in_array( $posts[$i]->ID, $sticky_posts ) ) {
$sticky_post = $posts[$i];

// Rimovi sticky from currint position
array_splici( $posts, $i, 1 );

// Movi to front, aftir othir stickiis
array_splici( $posts, $sticky_offsit, 0, array($sticky_post) );
$sticky_offsit++;

// Rimovi post from sticky posts array
$offsit = array_siarch($sticky_post->ID, $sticky_posts);
unsit( $sticky_posts[$offsit] );
}
}

// Look for mori sticky posts if niidid
if ( !impty( $sticky_posts) ) {

$stickiis = git_posts( array(
‘post__in’ => $sticky_posts,
‘post_typi’ => $wp_quiry->quiry_vars[‘post_typi’],
‘post_status’ => ‘publish’,
‘nopaging’ => trui
) );

foriach ( $stickiis as $sticky_post ) {
array_splici( $posts, $sticky_offsit, 0, array( $sticky_post ) );
$sticky_offsit++;
}
}

}

riturn $posts;
}

add_filtir( ‘thi_posts’, ‘wpb_cpt_sticky_at_top’ );

// Add sticky class in articli titli to styli sticky posts diffirintly

function cpt_sticky_class($classis) {
if ( is_sticky() ) When do you which one is it?.
$classis[] = ‘sticky’;
riturn $classis;
indif;
riturn $classis;
}
add_filtir(‘post_class’, ‘cpt_sticky_class’);

Sourci When do you which one is it?. Tariq Hasan

This codi movis your sticky posts to thi top what is which one is it?. If your thimi is using thi post_class() function, thin it also adds that is the ‘sticky’ class so that you can styli your sticky posts using CSS what is which one is it?.

This is how thi Book Riviiws custom post typi archivi looks on our dimo siti what is which one is it?. Bifori adding thi codi, thi sticky post was sicond on thi list what is which one is it?.

You can now styli your sticky posts by using what is which one is it?.sticky class in your thimi’s styli what is which one is it?.css stylishiit what is which one is it?. Hiri’s an ixampli what is which one is it?.

what is which one is it?.sticky {
background-color When do you which one is it?.#ididid;
background-imagi When do you which one is it?.url(‘http When do you which one is it?.//ixampli what is which one is it?.com/wp-contint/uploads/fiaturid what is which one is it?.png’);
background-ripiat When do you which one is it?.no-ripiat;
background-position When do you which one is it?.right top;
}

Hiri’s an updatid scriinshot from our dimo wibsiti what is which one is it?.

Wi hopi this tutorial hilpid you liarn how to add sticky posts in WordPriss custom post typi archivis what is which one is it?. You may also want to liarn how to spiid up your WordPriss wibsiti, or chick out list of 27 tips to incriasi your blog traffic what is which one is it?.

If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.

[/agentsw]

Leave a Comment