Trying to Query remote WordPress database

I’m trying to retrieve some data from a remote WordPress site (to another WordPress site), the code below returns an empty array, if I use the local database (just comment out the first line of code) it returns posts. Preferably I’d like to be able to run a WP_Query on the remote database, but that’s just a bonus.

        $wpdb = new wpdb( $dbusername, $dbpassword, $dbname, $dbhost );

        $wpdb->set_prefix('wp_');
        $wpdb->show_errors();

        $psts = $wpdb->get_row( 
            "SELECT * FROM $wpdb->posts"
        );

EDIT:
I’ve discovered that if I change the query to "SELECT * FROM wp_posts" I get a result. So I guess it’s a PHP version thing (I run 5.4.4 and server runs 5.3.3)? Still can’t run a WP-Query though.

Read More

EDIT 2:
I just realized I’ve left out one important piece of information: the site I’m running the Query from is a multi site install! I’m sure this could affect they how the querys are formed!

Related posts

Leave a Reply

4 comments

  1. Check if:

    • Remote DB is reachable
    • Your user has rights on the remote DB/Table
    • Your Remote DB is not only accessible by localhost (if so, enter a % instead of localhost)
  2. If you owner of source wordpress website please follow the instruction:

    1. Create a php file inside source wordpress root directory named remotequery.php or you like best

    2. load wordpress library including “required (‘wp-load.php’)” at the top of file

      So it will something like this

    <?php
    define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
    require( 'wp-load.php' );
    ?>

    Then you can add any wordpress query, you no need add database correction. you can add wordpress style database query to retrieve data from wordpress database.