Skip Navigation

[Resolved] Sort custom search results by post stickyness?

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to order View results by post stickiness.

Solution:
Because of the way WordPress identifies sticky posts, it is not possible include the stickiness of posts in the ordering options.

Sticky posts are stored in an array in wp_options, so you would need to run your query independent of their stickiness, then use the wpv_filter_query_post_process hook (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process) to manually re-order the results by first extracting those that are found in the array of sticky posts and then appending the remaining posts which are not.

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by a.R 6 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#612577

a.R

On my start page, I want to display the latest posts, the sticky ones first.
So far, I use 2 custom searches:
1. Display sticky posts
2. Display non-sticky posts.

Now I think, it would be better to just use one custom search and to sort it by stickyness first and THEN by date.
Should be same result, better performance, right?

Now I don´t find post stickyness among the sort criteria. Where is it?

THANK YOU, regards, Achim

#612728

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Achim

It would be possible to do this with a single query, but it requires custom code that processes the query results after the query has run.

Because of the way WordPress identifies sticky posts, it is not possible include the stickiness of posts in the ordering options.

Sticky posts are stored in an array in wp_options, so you would need to run your query independent of their stickiness, then use the wpv_filter_query_post_process hook (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process) to manually re-order the results by first extracting those that are found in the array of sticky posts and then appending the remaining posts which are not.

If you do this I doubt that you will see a significant performance improvement, because of how WordPress stores the sticky state of posts the initial database query must be followed by a post-query process to re-order the results.

If you try and run into problems, let me know.

https://codex.wordpress.org/Sticky_Posts

#613365

a.R

Ok, thank you.
Regards, Achim