Search another database with wp_query using new wpdb

Out of the box? No

You can do some trickery to try and fool WP_Query into using your new db object but it may have issues with cached objects etc

e.g.

global $wpdb;
// backup wpdb
$thedb = $wpdb;

// pretend mydb is actually wpdb
$wpdb = $mydb;

// do stuff

// reset wpdb back
$wpdb = $thedb;

However if you’re trying to pull in data from another site, I would strongly recommend against remote SQL queries, if not for performance reasons but for security reasons.

Have you considered RSS aggregation plugins?

Leave a Comment