How to properly escape values in meta query

The problem is that you’ve interpreted wpdb::esc_like as a literal copy paste, and not a direction, and used it as a static function, when it is not a static function.

Good:

global $wpdb;
$foo = $wpdb->esc_like( $bar );

Bad:

$foo = wpdb::esc_like( $bar );

Other things to note:

  • The & in &$wp_query ) is a holdover from PHP 4 that they fixed in PHP 5. However, a lot of WP devs weren’t sure and didn’t know this and copy pasted it everywhere. You should remove the &. The same with &$this, change it to $this. WordPress doesn’t even run on PHP 4.x
  • The filter you found was just that persons attempt that they were asking how to fix, there are other ways to do what you want
  • An actual search solution will massively outperform this without needing to write custom SQL. Look into solutions such as Elasticsearch, Solr, etc
  • Generally it’s far safer to use $wpdb->prepare, though sometimes that isn’t always possible. Needing to write SQL for WP tables is normally a warning sign that you’re doing something wrong.
  • If you used double quotes then you wouldn’t need to slash escape your single quotes inside the SQL string. E.g. "'test'" vs '\'test\''