SQL query to get adjacent posts from search query

Although this is possible, it really is not viable. I’ve done a lot of research on this subject, but could not find a straight forward answer

I went and had a look at the WP_Query class for a possible solution, and I came to the conclusion that what I was trying to accomplish would not be possible with a simple one or two liner code.

PITFALLS

  • To get the correct adjacent post, you have to match the original search query. Any deviation from the original search query will return incorrect adjacent posts

  • Single search terms are quite easy to replicate and in most cases worked perfectly. The problem arises with multiple search terms or sentences. This is handled very specifically by the WP_Query class.

  • There is a stopword filter in the WP_Query class which filters the search terms and remove these stopwords from the search query if they exist. So this would need to be replicated as well in a custom searh query for it to be consistent with the original search results

CONCLUSION

To get consistent and correct results, you have to copy the the complete search query section from the WP_Query class and incorporate it into the function in question. This is really not viable.

The best solution is to keep using WP_Query to keep the results consistent from the referrer pages and to get the correct adjacent posts. The method in the linked answer from the question is a bit bulky and redundant, and I’m busy rewriting it. Adjacent posts can be retrieved very easily by making use of the date_query parameters that was introduced in WordPress version 3.7