Sacconi69

New search query

Feb 21st, 2024
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. function bedrooms_guests_search_query( $query ) {
  2. if (! is_admin() && $query->is_main_query()) {
  3.  
  4. $custom_fields = array(
  5. // put all the meta fields you want to search for here
  6. "function_camere","function_ospiti"
  7.  
  8. );
  9. $searchterm = $query->query_vars['tipologia'];
  10.  
  11. // we have to remove the "s" parameter from the query, because it will prevent the posts from being found
  12. $query->query_vars['s'] = "";
  13.  
  14. if ($searchterm != "") {
  15. $meta_query = array('relation' => 'OR');
  16. foreach($custom_fields as $tipologia) {
  17. array_push($meta_query, array(
  18. 'key' => $tipologia,
  19. 'value' => $searchterm,
  20. 'compare' => 'LIKE'
  21. ));
  22. }
  23.  
  24. if ( array_key_exists('funzione_camere', $_GET ) && $searchterm != "")
  25.  
  26. if ( array_key_exists('funzione_ospiti', $_GET ) && $searchterm != "")
  27.  
  28. $query->set("meta_query", $meta_query);
  29. };
  30. }
  31. add_filter( "pre_get_posts", "bedrooms_guests_search_query");
  32.  
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment