I'm using Toolset Blocks with a custom post type called "Knowledge Hub". The archive currently shows articles in alphabetical order, but the client would like a select few to appear at the top of the list, whether viewing the archive for all or categories/search results.
I've installed the "Custom Post Type Sticky" plugin (https://wordpress.org/plugins/custom-post-type-sticky/) to add the function to custom post types. Now, I need to add the code to the archive template to display them at the top of the list, with all others below.
I tried adding a view to the archive, but the page froze up and said that nested views were not allowed.
How do I add the sticky posts to the archive, and exactly where?
What is the link to your site?
Working in the staging site: hidden link
I installed the plugin, but it doesn't appear to do anything, on my test site at least.
It isn't adding the ability to set custom posts as sticky, I don't see the UI anywhere, neither when editing posts with the classic editor nor with the block editor. Maybe something wrong with my test site.
I'm not sure what the point of the plugin is, though, because there are two parts to standard WP sticky posts. The first is the UI on the edit screens to mark a post as sticky. The second is actually bumping the sticky posts to the top of a list of posts when displaying them.
Even when working, all this plugin does is the first part. It doesn't do anything about the second (I checked its source code).
WordPress only applies sticky posts on the blog page, it doesn't apply the same technique anywhere else, so the idea of being able to mark custom posts as sticky seems redundant, unless you are going to also manipulate the query for custom post type archives to bring the sticky custom posts to the top (which it doesn't).
One solution with a custom archive would be to exclude sticky posts from the results and then add a View of those sticky posts before the archive results, but as you have discovered with Blocks nesting of Views is currently not supported.
Another solution (which the plugin author should probably include) would be to use a filter available in the core WP_Query class to pull out the sticky posts and elevate them to first place.
The logic for handling sticky posts within the WP_Query class can be found in the file wp-includes/class-wp-query.php from line 3131. It should be possible to reproduce this same logic in a callback function added to the_posts filter but which applies it on custom post archive pages rather than just on the blog home page.
After posting that, I wondered if there wasn't a better plugin for what you need, and sure enough there is, which handles both marking custom posts as sticky, and also displaying them sticky first on their archive pages: https://wordpress.org/plugins/sticky-posts-switch/
In response to your first reply, the plugin adds the checkbox to make the post sticky in the Publish > Visibility area of the page settings on the right side. So I can see that I've marked the post as sticky.
In the archive template, I also get the option of querying results down to either sticky or not sticky for the entire loop, but as you said, a nested view isn't allowed with Toolset blocks. In the Ordering Settings, there is not the option to sort first by sticky and then everything else (alphabetical, in our case) which would be ideal.
I will explore the other plugin you mention. For the benefit of others, I'll report back here what I find if you'll please keep this open.
Sure, let me mark this as awaiting your reply and you can update whether the second plugin meets your needs.
You should be able to use the plugin settings so that the sticky custom posts are shown first, and the subsequent posts will respect the ordering settings you specify in the custom archive, e.g. alphabetical.
I installed the Sticky Posts Switch and set the order for the Knowledge Hub articles to Date Published, per the client. Based on client needs, I checked the boxes to show sticky posts only on kh-category and kh-tag pages, not the overall listing. Works, with no code added to the template.
Now sticky posts ARE showing up for kh-categories and kh-tags , pulling the sticky post to the top EXCEPT for one category. WEIRD. I have been trying different things to no avail, I just can't figure out the reason it's working for all but one category.
You are correct - there is an article in two categories and it is marked as sticky. It appears at the top of one of the categories, but not the other. For example, the article "Intro to the LandPKS Habitat Module" is marked as Sticky. It appears as sticky on one category archive page for "LandPKS Modules & Functions": hidden link but not at the top of the other category "Habitat Information": hidden link
I really can't find a reason it wouldn't appear as sticky in both categories. Other articles are in two categories and show up as sticky in both.
I'm not sure what to suggest, aside from some simple basic steps such as re-saving the posts in question, removing then re-adding the taxonomy terms, un-marking as sticky then re-marking as sticky etc.
If nothing like that helps, I think you'd need to report the issue to the plugin developer.
The topic ‘[Closed] Add sticky posts to top of archive’ is closed to new replies.