4

Is there a way to hook into the WPDB instance ($wpdb)?

What i basically want, is to execute a own function BEFORE the desired query get's executed.

Is this possible?

Additional question for comment to first answer:

    $query = $wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "test WHERE type = %s and ip = %s", $type, $ip_full_log);
    $fetch = $wpdb->get_results($query, ARRAY_A);

Would this parse the final query to my callback function?

1 Answer 1

7

query - you will get sql as argument of the callback.

add_filter('query', 'some_callback_that_change_query');

function some_callback_that_change_query($sql){
    remove_filter('query', 'some_callback_that_change_query');
    // your banny wrote
    add_filter('query', 'some_callback_that_change_query');
    return $sql;
}
6
  • Thanks. I added a additional question to my original one. Please take a look at it. Many thanks!
    – TheNiceGuy
    Commented Jul 5, 2014 at 19:26
  • didn't really understand your additional question. With query filter you can hook to any query. Inside callback you can remove you hooked callback and set it after you proccess your code. is that you wanted? Commented Jul 5, 2014 at 20:35
  • I mean, do i have to use the query() function, or does this hook will also get called for the get_results() function, for example?
    – TheNiceGuy
    Commented Jul 6, 2014 at 10:22
  • this is a filter-hook - its only to change almost every sql query. but use can use it to run your sql query. Commented Jul 7, 2014 at 6:19
  • Sadly that was not my question. I can excute querys with multiple functions. e.g. with query() and get_results(). Will this filter get called for each of them?
    – TheNiceGuy
    Commented Jul 7, 2014 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.