How to order search results so a specific page appears first when user searches for a particular word?

Question

I am trying to create a function that would give me a specific result order when the user inserts a specific word in the search bar.

Example: The user inserts in the search bar “banana” and for now, my website comes up with all the results (pages, posts, whitepapers, …) that include “banana” which is fine, however, there are 3 specific pages amongst these results that I want to show to the user as the very first ones.

So what I want to achieve is: when the user inserts “banana” or “BANANA” query there will be

  1. a specific page with the ID 1025 displayed as first,
  2. a specific blogpost with the ID 21837 as second,
  3. a specific webinar with the ID 18495 as third,

and then other results will be listed in whatever order.

Thank you very much for your help!

function specific_page($query){
    if(!is_admin() && $query->is_main_query() && is_search() &&  $query==='BANANA')
    {
    
      $query->set('post__in', array(1025,21837,18495));
    }
 }
    add_filter('pre_get_posts','specific_page');

The above gives me the 3 search results for every query, not just “banana”…

0
Zuzana 2 weeks 2021-07-15T10:45:15-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse