WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem using "query" filter (5 posts)

  1. ketsugi
    Member
    Posted 7 years ago #

    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?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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;
    }

  3. ketsugi
    Member
    Posted 7 years ago #

    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.

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  5. ketsugi
    Member
    Posted 7 years ago #

    Otto42: that was the problem. Thanks :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags