Support » Plugins » Problem using “query” filter

  • Resolved ketsugi


    I’m trying to write a plugin that’ll log every SQL query WP sends (for troubleshooting purposes). Looking at the query() function in wp-includes/wp-db.php in trunk seems to show that all SQL queries are run through the “query” filter.

    So I added a function in my plugin called log_query(), and added this line in my plugin:

    add_filter('query', array('Developer', 'log_query'));

    However, this line causes WP to die for an unknown reason. Changing the filter name to any other known filter lets the PHP code run, but of course that doesn’t do what I want it to do. Is there any reason why this “query” filter doesn’t seem to be working?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    What’s with the array? That doesn’t make much sense.

    Try add_filter('query','log_query'); instead. You’ll then need your function to look like this:

    function log_query($query) {
    // do stuff with $query
    return $query;

    The array is when all the plugin’s functions are enclosed in a class (for namespacing purposes), where “Developer” is the name of the class. This isn’t the issue; it works in all the other add_action and add_filter calls. As I said, the problem is the “query” filter name.

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Hmmm… Is your function actually returning the $query? A filter is expected to modify text and return it. If you fail to return it, then you’re wiping out the query in the process.

    Otto42: that was the problem. Thanks 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Problem using “query” filter’ is closed to new replies.