Sticky posts in custom post type archives

26 thoughts on “Sticky posts in custom post type archives”

  1. Tareq, my question is this. I am using a commercial framework to create a Membership Real Estate listing site. Users can submit their own property listings after registering. This Framework adds custom functions and a User Front End that make sure that some of the prescribed values are contained in dropdowns or checkbox selections.

    Like most frameworks it keeps the Registered User from seeing and using the Wp Admin panel to make and edit the own posts.

    But, using this “glorified” FORM approach means that the User can not use or even see a Plugin or plugins written specifically to give every User with an Author or Subscriber role more ways to add content to their own Post/Listing.

    For example, I have an Availability Calendar plugin that after the Administrator installs and activates it then installs a User Menu that is seen in the WP Admin Panel in a Twenty-Eleven install.

    The Calendar plugin gives all logged in registered Users a manage_options permission for their own versions of the master Calendar. A User who has been assigned an AUTHOR role now will see a menu of different options they can create and save to create one separate instance of this Calendar for each listing/Post. Each Calendar then will have its own date ranges of Open and Closed days.

    BUT, in a Framework Front End it cannot show or access that Plugin for Users because FORMS cannot display PLUGINS and their Options.

    The Framework Front End is just a posting form, not a true WP Admin environment.

    Tareq, is there a way to allow a Framework to have both the “pretty Front End for User interface” but ALSO allow this Form to integrate “parts” of the WP User Admin — specifically the full Text Editor with any tools that a Plugin would install AND the Plugin Options Menu as seen in the Wp Admin view if the User were in the default WP Admin logged in view?

    Can a Framework Front End be a HYBRID of both simplified User Interface and the more sophisticated User Administration of a for-User Plugin?

    Thanks for considering this lengthy question!

    1. Yeah, thats obviously possible. Creating a backend area and frontend area for maintaining things is a twice pain, cause you’ve to deal with both area. It’s painful, but possible.

  2. Hi Tareq,

    Thanks for this post. It’s quite useful.

    One short-coming of this function that I think I’m seeing is that if the sticky post does not appear in the first “page” as determined by the ‘posts_per_page’ setting of the query, then the_posts filter is too late to add it.

    For example, if I am listing the three most-recent posts and the fourth most-recent post is sticky, then it seems not to work.

    Did you run into this and can you think of how you might handle it. My current thought is to actually query for all the sticky posts and then add each post object onto the front of $posts object while popping off on from the end each time. However, if one needed page (I don’t, but still…) then I don’t think this would work.

    1. Yeah, you got it right. The trick I used here is, the_post returns the current posts. I get the list of sticky posts, match them with the posts and pop out the found one from their current position and place them to front. I think WordPress does that same thing.

      It actually depends on the situation. If you really need to display all the sticky posts at the top, pre_get_posts filter would be ideal to deal with besides of this trick.

      1. Hmm… I also need the functionality of grabbing posts on pages other than the current one. Simply changing ‘the_posts’ to ‘pre_get_posts’ doesn’t work for me, though. Are there additional implied steps that I’m missing?

        1. May be it’s not working because you are doing it on a page. If you check the code, it’s using is_main_query() to determine if it’s the main loop. In a page, the main loop only will return one post. So using the same code won’t work there. In that case, may be you need to run a new WP_Query instance and remove the is_main_query() checking.

          1. I misspoke—I’m running this on a custom post type archives page. It’s really the exact same predicament Mark describes. When I said “on pages other than the current one,” I meant to describe the pagination.

          2. Till now I thought, if you set the pagination to 10 posts per page, WP will show only 10 posts in a page including the sticky posts. And I was wrong. Simply WP pulls the sticky posts and prepends to the front.

            So we get the idea, now code is updated 🙂

  3. Hey Tareq,

    Thanks for this very useful code. I have a question though. On the home page the sticky posts usually have class .sticky or .featured-post added by WordPress. Can we get them in this hack or how do you suggest that we should style the sticky posts differently?

    1. I think it’ll add the sticky class automatically, just you need to make sure that you are using the post_class() function in your themes template file.

      1. yes, I am using default twenty twelve theme which uses post_class.


        <article id="post-" >

        which outputs these classes:

        post-6 movie_reviews type-movie_reviews status-publish hentry

        but not sticky.

        1. Oh you are right, I missed the a part on the get_post_class() function. Seems like it’s adding the sticky class only on the homepage. So you need to add a filter on post_class to add the sticky css class.

          1. Thanks, I used this to add sticky class:

  4. Tareq,
    many thanks for this snippet – it is really helpful and easy to understand.

    I am considering one small problem though – if you put this code to your functions.php and (for whatever reason) want your posts to be displayed in a random order, how do you make the sticky posts stay at the top and randomize the regular ones?

    Adding query_posts($query_string . ‘&orderby=rand’); to my archive file randomizes the order of all the posts, so the sticky posts won’t stay at the top.

    Kind regards

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.