Title: Add getInstance() Function to Objects
Last modified: March 27, 2020

---

# Add getInstance() Function to Objects

 *  Resolved [Kipperlenny](https://wordpress.org/support/users/kipperlenny/)
 * (@kipperlenny)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/add-getinstance-function-to-objects/)
 * I would like to remove a filter your Where Class is adding – but you call do 
   not use the getInstance pattern (many new wordpress Plugins use this!). And you
   are not assigning the Object to a variable.
 * Because of that, it’s not possible to remove_filter().
 * Please read this comment: [https://wordpress.stackexchange.com/a/36110](https://wordpress.stackexchange.com/a/36110)
 * And add this to your classes:
 *     ```
           protected static $instance = NULL;
   
           public static function getInstance() {
               NULL === self::$instance and self::$instance = new self;
               return self::$instance;
           }
       ```
   
 * or at least assign the Objects to a variable:
 *     ```
           public function initSearch()
           {
             if (!$this->isSearchAvailable()) return;
   
             new Join();
             new Query();
             new Request();
             new Where();
           }
       ```
   
 * Because otherwise, remove_filter f.e. for
 * `add_filter('posts_search', [$this, 'sqlWhere'], 0, 2);`
 * from your Search\Where class is not working in themes functions.php
 * I am using [https://github.com/herewithme/wp-filters-extras/](https://github.com/herewithme/wp-filters-extras/)
   now to do this, but it should not be needed anymore today.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Mateusz Gbiorczyk](https://wordpress.org/support/users/mateuszgbiorczyk/)
 * (@mateuszgbiorczyk)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/add-getinstance-function-to-objects/#post-12591779)
 * Hi [@kipperlenny](https://wordpress.org/support/users/kipperlenny/),
 * Thank you for your message.
 * I agree with you. I have prepared a solution that could help you. Could you check
   them out?
 * Please download the plugin here:
    [https://wetransfer.com/downloads/7bf3ed82cbab7ce935c782f7a8a243a420200327142952/4e19e65dacf243529b2e0cd89af197c120200327142952/1aeb48](https://wetransfer.com/downloads/7bf3ed82cbab7ce935c782f7a8a243a420200327142952/4e19e65dacf243529b2e0cd89af197c120200327142952/1aeb48)
 * Then to disable the filter use the following code:
 *     ```
       add_action('init', function()
       {
         remove_filter('posts_search', ['AcfBetterSearch\Search\Where', 'sqlWhere']);
       });
       ```
   
 * Does this solve the problem? Do you have any other suggestions? I will be grateful
   for your every idea.
 *  Thread Starter [Kipperlenny](https://wordpress.org/support/users/kipperlenny/)
 * (@kipperlenny)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/add-getinstance-function-to-objects/#post-12591790)
 * I did not install it – because I guess it would fail. You cannot use $this in
   static functions!
 *  Plugin Author [Mateusz Gbiorczyk](https://wordpress.org/support/users/mateuszgbiorczyk/)
 * (@mateuszgbiorczyk)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/add-getinstance-function-to-objects/#post-12591928)
 * Oh, forgive me for this mistake. Please try again:
    [https://wetransfer.com/downloads/8f194d854e668963eaa85a46d9c6a47920200327150210/f88dcdcb7d177e3f57c746cc51916b9d20200327150210/148d2c](https://wetransfer.com/downloads/8f194d854e668963eaa85a46d9c6a47920200327150210/f88dcdcb7d177e3f57c746cc51916b9d20200327150210/148d2c)
 *  Thread Starter [Kipperlenny](https://wordpress.org/support/users/kipperlenny/)
 * (@kipperlenny)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/add-getinstance-function-to-objects/#post-12592010)
 * Yes it did work, now it’s way easier to remove the filter.
 * But for the future… Try to use getInstance() please for every object 🙂
    -  This reply was modified 6 years, 2 months ago by [Kipperlenny](https://wordpress.org/support/users/kipperlenny/).
    -  This reply was modified 6 years, 2 months ago by [Kipperlenny](https://wordpress.org/support/users/kipperlenny/).
 *  Plugin Author [Mateusz Gbiorczyk](https://wordpress.org/support/users/mateuszgbiorczyk/)
 * (@mateuszgbiorczyk)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/add-getinstance-function-to-objects/#post-12592054)
 * As you can see in the code, I only use static methods in these classes, so this
   problem with the E_DEPRECATED error will not occur.
 *  Thread Starter [Kipperlenny](https://wordpress.org/support/users/kipperlenny/)
 * (@kipperlenny)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/add-getinstance-function-to-objects/#post-12592056)
 * Yes, I edited my post already 😀
    Thanks for your help!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Add getInstance() Function to Objects’ is closed to new replies.

 * ![](https://ps.w.org/acf-better-search/assets/icon-256x256.png?rev=2636287)
 * [ACF: Better Search](https://wordpress.org/plugins/acf-better-search/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/acf-better-search/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/acf-better-search/)
 * [Active Topics](https://wordpress.org/support/plugin/acf-better-search/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/acf-better-search/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/acf-better-search/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [Kipperlenny](https://wordpress.org/support/users/kipperlenny/)
 * Last activity: [6 years, 2 months ago](https://wordpress.org/support/topic/add-getinstance-function-to-objects/#post-12592056)
 * Status: resolved