I need to make Wordpress use another database (not the one specified in wp-config site-wide) on the "Comments" admin backend. I then must be able to edit, approve, delete etc. comments as usual. The database server I am going to connect to has been setup to accept external connections.

I have a couple of ideas how to approach this:

  1. from wp-config.php: have an if block to change the database connection strings if user is viewing comments page.

However this doesn't sound good to me, I assume this may create a lot of unnecessary database connections if user goes back and forth to the comments page.

  1. modify wp-admin files: create an instance of wpdb and tell Wordpress to use it for comment administration.

However, I can't find the .php files that get the comments from the database (so I can change the database where they do it). Have been looking at edit-comments.php->class-wp-comments-list-table.php->class-wp-list-table.php (down to base class), but I can't see the bit that talks to the database.

  1. Any hooks available to tap into and change the database? (doubt it)

Many thanks in advance.

2 Answers 2


You can filter query. See wpdb::query() in wp-includes/wp-db.php.

You get the complete SQL string as parameter. Then look for queries to the comments table:

add_filter( 'query', function( $query ) 
    global $wpdb;

    if ( FALSE === stripos( $query, "FROM $wpdb->comments" )
        return $query;

    // Now change the SQL

But you have to catch all the JOINs with other tables too. Not sure if this is possible in an elegant way.

  • Ah, what a nice filter! Thinking about replacing $wpdb->comments with $wpdb_2->comments for every query to the comments table. So if a JOIN was to happen, it should replace it in the JOIN too - what is your concern about JOIN?
    – Sergey
    Commented Jun 1, 2013 at 15:31
  • @Sergiy If you use another database, and not just another table, you cannot use the current connection for the new comments table. But you need that connection for the posts table.
    – fuxia
    Commented Jun 1, 2013 at 17:20
  • Yes, of course, thanks for pointing out! I am thinking it might be easier to write a plugin and use WP's built-in functions to approve/spam/trash/edit comments. Otherwise I have to filter out every query for comment action and who knows what level of complexity those SQL queries are.
    – Sergey
    Commented Jun 1, 2013 at 17:47

The issue with trying to subvert whole database access is that even on page, dealing with comments, WP will still need and perform queries for other data. The challenge is not as much changing where query go, as changing it without everything falling apart.

Your best chance it probably to try some solution for sharding on WP level (such as HyperDB or SQL level).

PS it would help to suggest possible alternatives if you included context why do you need (or think you need) separate table for comments. There are people around that have experience dealing with large installations and who probably have more precise answer than generic theory.

  • I don't think WP will need to do any processing other than updating the comments table? The database is mirrored live from another server. This was designed by the server provider to limit any interaction with the live server, as the server contains some critical and sensitive data etc. Admin works with WP using the backend on cluster, data then replicates to the live site. Problem is that comments are made on the live site and not the one on the cluster. Cluster WP only connects to its local db that doesn't have those comments. I don't think it is possible to redesign this system now :)
    – Sergey
    Commented Jun 1, 2013 at 15:38

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.