2

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?

5

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;
}
  • Thanks. I added a additional question to my original one. Please take a look at it. Many thanks! – TheNiceGuy Jul 5 '14 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? – Butuzov Jul 5 '14 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 Jul 6 '14 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. – Butuzov Jul 7 '14 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 Jul 7 '14 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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