Forums › Forums › Search & Filter Pro › Exclude Sticky Post from Result Page
- This topic has 1 reply, 2 voices, and was last updated 4 years, 3 months ago by Trevor.
-
Anonymous(Private) August 14, 2020 at 7:41 am #256024
Hello
I have some sticky post that is listed at the top of my blog page. The post are filtered based on category. Can you please let me know how could we remove those sticky posts from the result page if that does not fall under the selected category?
Thanks
Trevor(Private) August 14, 2020 at 8:48 am #256040That would be tricky, as the WordPress ‘sticky’ functionality works the way you are observing, which means that they are not filtered out in a search. When you do a standard WordPress search, theme search templates thus ignore sticky completely to avoid this problem.
There IS a way, but is involved.
You will require a custom field with some form of logical value (yes/no, true/false, on/off, 1/0) that represents whether you want the post at the start of results.
EVERY post (of the type you are searching) MUST have a value stored in this custom field. If they do not, they will disappear from the results.
In the form design, on the Posts settings tab, you would set the form to ignore sticky posts, and then set a primary sort (order by) on that custom field (Post Meta/Meta Key) either ascending or descending (whichever places the desired posts first). Then set a secondary sort by title or whatever suits your needs.
This does require that, after creating the custom field, you edit every existing post and set the value accordingly. For new posts, and for posts that you edit and save in the WordPress editor, you could set up a WordPress autosave function (using your child theme functions.php file) whereby that function checks if the post is of the post type desired, and whether it is sticky, and would then autosave the correct value to that custom field, thus saving the need to do that manually.
A bit complicated, but it is the best workaround I can think of.
It might be also be possible to set ignore sticky in the form (as per the previous suggestion) and use the WordPress pre_get_posts() function in your child theme functions.php file to somehow fetch the list of posts of that type that are sticky, compare that to the list of post IDs in the results, and then re-order those posts, but I do not know how that can actually be done.
-
AuthorPosts